简体   繁体   English

在C3.js或D3.js中创建自定义列表图表

[英]Creating a Custom List Chart in C3.js or D3.js

I'm trying to create a custom chart using either C3.js or D3.js but am not sure how to approach this. 我正在尝试使用C3.jsD3.js创建自定义图表,但不确定如何实现此目的。 Below is an example of how the chart should output... 以下是图表应如何输出的示例...

列表图

I've had a look at the examples provided by C3.js & D3.js and found that some of the charts had the names of the data input at the bottom of the chart, for example.. 我看了看C3.jsD3.js提供的示例,发现某些图表例如在图表底部具有输入数据的名称。

饼图标题示例

Is there someway I can override these to produce the desired chart or am I approaching this incorrectly? 是否可以通过某种方式覆盖这些内容以生成所需的图表,或者我是否错误地接近了该图表? How would I create this Custom List Chart? 我将如何创建此自定义列表图表?

This kind of a list chart can be created with d3 fairly easily. 使用d3可以很容易地创建这种列表图。 You can use a simple data object to generate it. 您可以使用一个简单的数据对象来生成它。 Below, I've created rects for the left and right separately and then added the labels. 在下面,我分别为左右创建了rect,然后添加了标签。 The color of the bars on the right is based on the status field in the data object. 右侧条形的颜色基于数据对象中的status字段。

Check out this fiddle: https://jsfiddle.net/qf9po0L5/ 看看这个小提琴: https : //jsfiddle.net/qf9po0L5/

svg.selectAll("bar")
  .data(data)
  .enter().append("rect")
  .attr("x", 10)
  .attr("width", 200)
  .attr("y", function(d, i) {
    return i * 32
  })
  .attr("height", 30)
  .attr("fill", function(d, i) {
    return i % 2 ? "#83adef" : "lightblue";
  });

svg.selectAll("second-bar")
  .data(data)
  .enter().append("rect")
  .style("fill", function(d) {
    return d.status === 0 ? "red" : "green";
  })
  .attr("x", 212)
  .attr("width", 20)
  .attr("y", function(d, i) {
    return i * 32
  })
  .attr("height", 30);


var yTextPadding = 20;
svg.selectAll(".bartext")
  .data(data)
  .enter()
  .append("text")
  .attr("class", "bartext")
  .attr("text-anchor", "middle")
  .attr("fill", "white")
  .attr("x", 55)
  .attr("y", function(d, i) {
    return (i * 32) + 22;
  })
  .text(function(d) {
    return d.name;
  })
  .attr("fill", "black");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM