【问题标题】:How to make stack bar graph in SVG from JSON data?如何从 JSON 数据制作 SVG 中的堆栈条形图?
【发布时间】:2021-05-11 14:58:40
【问题描述】:

我正在回复@markb profile 提出的question,但问题已被删除。我在这里分享我的解决方案。

目标是将 JSON 数据转换成这样的图表条:

JSON 数据是这样格式化的:

// dummy data
const json = `{ "data": [ 
    { "value": [1, 2, 3, 4] }, 
    { "value": [5, 6, 7, 8] },
    { "value": [9, 0, 1, 2] },
    { "value": [3, 4, 5, 6] } 
] }`,
  dataJSON = JSON.parse(json).data;

【问题讨论】:

  • @markb 如果你还需要它

标签: javascript html svg


【解决方案1】:

我可以提出这个解决方案。

// dummy data
const json = `{ "data": [ 
{ "value": [1, 2, 3, 4] }, 
{ "value": [5, 6, 7, 8] },
{ "value": [9, 0, 1, 2] },
{ "value": [3, 4, 5, 6] } 
] }`,
dataJSON = JSON.parse(json).data;

const graphDOM = document.getElementById("graphDOM");

const imageHeight = 100;
const colWidth = 10;
const spaceBetweenCols = 20;

// this will contain the sum for each column
const totalColumns = [];

// this is the highest a column will go
maxValueAllColumns = 0;

// this is the number of columns that will be displayed
nbColumns = 0;


for (line=0; line<dataJSON.length; line++) {
  for (col=0; col<dataJSON[line]["value"].length; col++) {
    if (typeof totalColumns[col] == "undefined") {
      totalColumns[col] = 0;
    }
    totalColumns[col]+=dataJSON[line]["value"][col];
    maxValueAllColumns=Math.max(maxValueAllColumns,totalColumns[col]);
    nbColumns = Math.max(nbColumns, col+1);
  }
}

// ratio
ratioDisplayed = imageHeight / maxValueAllColumns;

// variables
dataContainer = document.getElementById('data-container');
dataItem = document.createElementNS('http://www.w3.org/2000/svg', 'g');

html="";
for (col=0; col<nbColumns; col++) {
  stackTo = 0;
  for (line=0; line<dataJSON.length; line++) {
    if (typeof dataJSON[line]["value"][col] != "undefined") {
      rectHeight = Math.round(dataJSON[line]["value"][col] * ratioDisplayed);
      rect_x0 = (spaceBetweenCols+colWidth)*col;
      rect_y0 = 0 + imageHeight - stackTo - rectHeight;
      stackTo += rectHeight;
      
      html += '<rect x="' + rect_x0 + '"y="' + rect_y0 + '"width="' + colWidth + '" height="' + rectHeight + '" fill="rgb(' + (50*line) + ', ' + (50*line) + ', ' + (50*line) + ')"></rect>"';
    }
  }
}

graphDOM.innerHTML = "<svg>" + html + "</svg>";
svg {
  max-width: 25em;
  margin: 2em 5em;
  overflow: visible !important;
}
<div id="graphDOM"><svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet">
    <g id="data-container"></g>
</svg>
</div>

【讨论】:

  • 由于缺少&lt;/svg&gt;,它似乎无法正常工作。补充说。
  • Javascript 无论如何都应该删除 div 中的所有内容,但是谢谢,它更干净了。 :) 我没有关注如何以最佳方式显示 html,所描述的问题更多的是关于如何构建图表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
  • 2019-08-09
  • 2018-09-28
相关资源
最近更新 更多