Skip to content

Commit

Permalink
条形图数据处理
Browse files Browse the repository at this point in the history
  • Loading branch information
zionuke committed May 5, 2023
1 parent 0c638a4 commit 2f4780f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules
44 changes: 44 additions & 0 deletions code/ch03/barchart/canvas/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 数据处理
const data = [
{ name: 'questions', value: 17 },
{ name: 'schools', value: 25 },
{ name: 'philosophers', value: 35 },
];

const chartWidth = 480; // 条形图的宽度
const chartHeight = 300; // 条形图的高度
const margin = 15; // 条形图的外边距

const containerWidth = chartWidth + margin * 2; // 容器的宽度
const containerHeight = chartHeight + margin * 2; // 容器的高度

const names = Array.from(data, (d) => d.name);
const values = Array.from(data, (d) => d.value);
const indices = Array.from(data, (_, i) => i);

// 布局
// 计算每一个条左下顶点的横坐标
// 位置和在数组里面的 index 有关
const step = chartWidth / names.length;
const barWidth = step * 0.8;
const xs = Array.from(indices, (i) => i * step);

// 计算每一个条左下顶点的纵坐标 ?
// 因为所有条底部都是对齐的,所以就是图表的高度
const y = chartHeight;

// 映射
// 获得每一个条的高度
// 条的高度应该和 value 线性相关的
const vmax = Math.max(...values);
const barHeights = Array.from(values, (v) => chartHeight * (v / vmax));

// 获得每一个条的颜色
const nameColor = {
questions: "#5B8FF9",
philosophers: "#61DDAA",
schools: "#65789B",
};
const colors = Array.from(names, (name) => nameColor[name]);

// 数据渲染

0 comments on commit 2f4780f

Please sign in to comment.