当前位置:首页 > JavaScript

js导入数组实现柱状图

2026-01-31 10:50:11JavaScript

使用Chart.js库绘制柱状图

安装Chart.js库

npm install chart.js

导入Chart.js和数组数据

import { Chart } from 'chart.js/auto';

const data = {
  labels: ['一月', '二月', '三月', '四月', '五月'],
  datasets: [{
    label: '月度销售额',
    data: [65, 59, 80, 81, 56],
    backgroundColor: 'rgba(54, 162, 235, 0.5)',
    borderColor: 'rgba(54, 162, 235, 1)',
    borderWidth: 1
  }]
};

创建柱状图

js导入数组实现柱状图

const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
  type: 'bar',
  data: data,
  options: {
    responsive: true,
    scales: {
      y: {
        beginAtZero: true
      }
    }
  }
});

使用纯JavaScript和Canvas绘制柱状图

准备HTML结构

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

JavaScript实现代码

js导入数组实现柱状图

const canvas = document.getElementById('barChart');
const ctx = canvas.getContext('2d');

const data = [45, 76, 32, 89, 54];
const labels = ['A', 'B', 'C', 'D', 'E'];
const colors = ['#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF'];

const maxValue = Math.max(...data);
const barWidth = 50;
const gap = 30;
const startX = 60;

ctx.font = '12px Arial';

// 绘制柱状图
data.forEach((value, index) => {
  const barHeight = (value / maxValue) * 200;
  const x = startX + index * (barWidth + gap);
  const y = canvas.height - barHeight - 30;

  ctx.fillStyle = colors[index];
  ctx.fillRect(x, y, barWidth, barHeight);

  // 添加数据标签
  ctx.fillStyle = '#000';
  ctx.fillText(labels[index], x + barWidth/2 - 5, canvas.height - 10);
  ctx.fillText(value, x + barWidth/2 - 5, y - 5);
});

// 绘制坐标轴
ctx.beginPath();
ctx.moveTo(30, 20);
ctx.lineTo(30, canvas.height - 30);
ctx.lineTo(canvas.width, canvas.height - 30);
ctx.stroke();

使用D3.js库绘制柱状图

安装D3.js

npm install d3

D3.js实现代码

import * as d3 from 'd3';

const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
const width = 600;
const height = 400;
const padding = 40;

const svg = d3.select('body')
  .append('svg')
  .attr('width', width)
  .attr('height', height);

const xScale = d3.scaleBand()
  .domain(d3.range(dataset.length))
  .range([padding, width - padding])
  .padding(0.1);

const yScale = d3.scaleLinear()
  .domain([0, d3.max(dataset)])
  .range([height - padding, padding]);

svg.selectAll('rect')
  .data(dataset)
  .enter()
  .append('rect')
  .attr('x', (d, i) => xScale(i))
  .attr('y', d => yScale(d))
  .attr('width', xScale.bandwidth())
  .attr('height', d => height - padding - yScale(d))
  .attr('fill', 'steelblue');

// 添加坐标轴
svg.append('g')
  .attr('transform', `translate(0, ${height - padding})`)
  .call(d3.axisBottom(xScale));

svg.append('g')
  .attr('transform', `translate(${padding}, 0)`)
  .call(d3.axisLeft(yScale));

以上三种方法均可实现基于数组数据的柱状图绘制,开发者可根据项目需求选择合适的实现方式。Chart.js适合快速实现标准图表,纯JavaScript适合轻量级需求,D3.js则提供更强大的数据可视化功能。

标签: 数组柱状图
分享给朋友:

相关文章

jquery数组

jquery数组

jQuery 数组操作 jQuery 提供了多种方法来操作数组或类数组对象(如 jQuery 对象集合)。以下是常见的数组操作方法: 遍历数组 使用 $.each() 方法可以遍历数组或对象: v…

java如何定义一个数组

java如何定义一个数组

定义数组的基本语法 在Java中,数组是固定长度的同类型数据集合。定义数组需要指定数据类型和数组名称,并可以选择直接初始化或稍后分配空间。 // 声明数组但不初始化 数据类型[] 数组名; //…

vue实现数组倒叙

vue实现数组倒叙

实现数组倒序的方法 在Vue中实现数组倒序可以通过多种方式完成,以下是几种常见的方法: 使用JavaScript的reverse()方法 // 在data中定义数组 data() { retu…

vue实现嵌套数组

vue实现嵌套数组

Vue 中实现嵌套数组的常见方法 在 Vue 中处理嵌套数组时,通常会结合 v-for 指令和响应式数据管理。以下是几种典型场景的实现方式: 基本嵌套渲染 使用多层 v-for 渲染嵌套数组结构:…

vue实现数组置顶操作

vue实现数组置顶操作

实现数组置顶的方法 在Vue中实现数组置顶操作,可以通过多种方式完成。以下是几种常见的实现方法: 使用数组的splice和unshift方法 // 假设有一个数组items const ind…

vue 数组响应式实现

vue 数组响应式实现

Vue 数组响应式实现原理 Vue 通过重写数组的原型方法来实现对数组操作的响应式监听。具体实现基于以下机制: 拦截数组变异方法 Vue 创建了一个继承自原生 Array 原型的对象,并重写了以下…