当前位置:首页 > JavaScript

js实现柱状图

2026-04-04 03:52:49JavaScript

使用原生JavaScript实现柱状图

在HTML中创建画布元素

<canvas id="barChart" width="400" height="300"></canvas>

获取画布上下文并设置基础样式

const canvas = document.getElementById('barChart');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#3498db';
ctx.strokeStyle = '#2980b9';
ctx.lineWidth = 2;

定义数据和图表参数

const data = [45, 72, 38, 64, 89];
const barWidth = 50;
const gap = 30;
const startX = 60;
const baseY = canvas.height - 50;

绘制柱状图

data.forEach((value, index) => {
    const x = startX + index * (barWidth + gap);
    const barHeight = (value / 100) * (baseY - 50);

    ctx.fillRect(x, baseY - barHeight, barWidth, barHeight);
    ctx.strokeRect(x, baseY - barHeight, barWidth, barHeight);

    ctx.fillStyle = '#2c3e50';
    ctx.font = '14px Arial';
    ctx.fillText(value, x + barWidth/2 - 10, baseY - barHeight - 10);
    ctx.fillStyle = '#3498db';
});

添加坐标轴和标签

js实现柱状图

ctx.beginPath();
ctx.moveTo(40, 30);
ctx.lineTo(40, baseY);
ctx.lineTo(canvas.width - 20, baseY);
ctx.strokeStyle = '#7f8c8d';
ctx.stroke();

ctx.fillStyle = '#2c3e50';
ctx.fillText('数值', 20, 20);
ctx.fillText('类别', canvas.width - 40, baseY + 30);

使用Chart.js库实现柱状图

引入Chart.js库

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

准备HTML容器

<div style="width: 80%; margin: 0 auto;">
    <canvas id="myChart"></canvas>
</div>

创建柱状图实例

js实现柱状图

const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['一月', '二月', '三月', '四月', '五月'],
        datasets: [{
            label: '月度销售额',
            data: [65, 59, 80, 81, 56],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)'
            ],
            borderColor: [
                'rgba(255, 99, 132, 1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            y: {
                beginAtZero: true
            }
        }
    }
});

响应式柱状图实现

添加窗口大小变化监听

window.addEventListener('resize', function() {
    myChart.resize();
});

设置图表容器响应式样式

.chart-container {
    position: relative;
    width: 100%;
    min-height: 300px;
}

动态数据更新

添加数据更新方法

function updateChart(newData) {
    myChart.data.datasets[0].data = newData;
    myChart.update();
}

示例数据更新调用

setTimeout(() => {
    updateChart([30, 45, 60, 75, 90]);
}, 2000);

标签: 柱状图js
分享给朋友:

相关文章

js实现列表

js实现列表

使用 JavaScript 实现列表 JavaScript 提供了多种方式来实现列表功能,包括数组操作、DOM 元素动态生成等。以下是几种常见的实现方法: 使用数组存储列表数据 数组是 JavaSc…

js实现选题

js实现选题

实现选题功能的JavaScript方法 基础实现方案 使用数组存储选项,通过随机索引选取: const options = ['选项A', '选项B', '选项C', '选项D']; const r…

js实现vue路由

js实现vue路由

Vue 路由的基本实现 在 Vue.js 中实现路由功能通常使用 Vue Router 库。Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue R…

js实现文字滚动

js实现文字滚动

实现文字滚动的几种方法 使用CSS动画实现滚动 通过CSS的@keyframes和transform属性可以实现平滑的文字滚动效果。 <style> .scroll-text { w…

js实现菜单

js实现菜单

实现基本HTML结构 使用HTML创建菜单的基本框架,通常包含<ul>和<li>元素。示例结构如下: <ul id="menu"> <li><…

js实现下拉刷新

js实现下拉刷新

监听触摸事件 通过监听 touchstart、touchmove 和 touchend 事件来检测用户下拉手势。记录触摸起始位置和移动距离。 let startY = 0; let curr…