javascript - 使用 C3 在 x 轴上使用单独的列制作简单的条形图

标签 javascript d3.js charts c3.js

我正在尝试使用 C3 和 D3 创建条形图,但我无法让列彼此无关,除了它们在 Y 轴上使用相同的刻度这一事实。

为了更好地解释,我添加了图片。

var chart = c3.generate({
    bindto: '#designerChart',
    data: {

      columns: [
        ['MA', 6],
        ['ME', 8],
        ['NY', 6],
        ['CN', 5],
        ['TX', 2]
      ],
      type: 'bar',

    },
    axis: {
        y: {
            max: 10,
            min: 0,
            padding: { top: 0, bottom: 0 }
        }
    }
});

结果是一组条形图,当我将鼠标悬停在它们上方时,我得到了所有条形图的详细信息 - 这不是我想要的。

Bar chart but grouped like a stepped column

我可以更改数据,以便它显示单独的列,但它们的颜色相同,当我想将其转换为饼图时,您无法区分状态。

var chart = c3.generate({
    bindto: '#designerChart',
    data: {
      x: 'x',
      columns: [
        ['x','MA', 'ME', 'NY', 'CN', 'TX'],
        ['rainfall', 6, 8, 6, 5, 4 ]
      ],
      type: 'bar',

    },
    axis: {
        x: {
            type: 'category'
        },
        y: {
            max: 10,
            min: 0,
            padding: { top: 0, bottom: 0 }
        }
    }
});

chart, but not different colors pointless pie chart

这就是我想要的:

enter image description here

最佳答案

条形图解决方案:

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.16.0/d3.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.7.20/c3.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.7.20/c3.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>C3 Bar Chart</title>
</head>

<body>
  <div id="designerChart"></div>

  <script type="text/javascript" charset="utf-8">
    $(document).ready(function() {

      columnColors = ['#9a4d6f', '#c76c47', '#f85115', '#d9b099', '#d4ba2f'];

      function setColumnBarColors(colors, chartContainer) {

        $('#' + chartContainer + ' .c3-chart-bars .c3-shape').each(function(index) {
          this.style.cssText += 'fill: ' + colors[index] + ' !important; stroke: ' + colors[index] + '; !important';
        });

        $('#' + chartContainer + ' .c3-chart-texts .c3-text').each(function(index) {
          this.style.cssText += 'fill: ' + colors[index] + ' !important;';
        });
      }

      var chart = c3.generate({
        bindto: '#designerChart',
        data: {
          columns: [
            ['rainfall', 6, 8, 6, 5, 4]
          ],
          type: 'bar'
        },
        axis: {
          x: {
            label: {
              text: 'States',
              position: 'outer-center',
            },
            type: 'category',
            categories: ['MA', 'ME', 'NY', 'CN', 'TX'],
            tick: {
              centered: true
            }
          },
          y: {
            label: {
              text: 'Rainfall (inches)',
              position: 'outer-middle'
            },
            max: 10,
            min: 0,
            padding: {
              top: 0,
              bottom: 0
            }
          }
        },
        legend: {
          show: false
        }
      });

      setColumnBarColors(columnColors, 'designerChart');

      // Color turns to original when window is resized
      // To handle that
      $(window).resize(function() {
        setColumnBarColors(columnColors, 'designerChart');
      });
    });
  </script>

</body>

</html>

在此 fiddle (整页)中,颜色变为原始颜色。但它会根据工作本地文件以及 c3、d3 和 jquery 的本地引用改变颜色。

引用资料:

关于javascript - 使用 C3 在 x 轴上使用单独的列制作简单的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33315769/

相关文章:

javascript - 缩小删除日期但保留时间

javascript - onClick on commandLink 设置该链接所附图像的 css

javascript - 检查菜单链接是否有子菜单 JavaScript

javascript - 使用对话框从 python 中的站点下载文件

javascript - D3 TreeMap 布局调整大小

javascript - 使用两组的 dc.js 箱形图缩减器

javascript - D3 时间和日期直方图

javascript - 在 React 中使用 CHild 中的 onclick 更新父级中的状态

d3.js - d3 donut /饼图 - 在圆弧之间画一条线

javascript - 使用基于 json 的配置在 amCharts 中创建标签