【问题标题】:Add space Between Columns in Bar chart. chartjs在条形图中的列之间添加空格。图表
【发布时间】:2020-04-08 17:17:37
【问题描述】:

我正在使用图表 js V 2.9.3 创建图表。 当我用少量数据创建图表时,它可以完美地呈现数据,但是当数据量增加时,图表变得拥挤。

图表在单个标签中有两列。

我也无法在不旋转的情况下设置标签。

var config = {
    type: 'bar',
    data: {
      labels: _datesForLabel,
      datasets: _chartDataWithOptions,          
    },
    options: {
      tooltips: {
      },
      plugins: {
        colorschemes: {
          scheme: 'office.Waveform6'
        }
      },
      scales: {
        yAxes: [{
          ticks: {
            min: 0,
          }
        }],
        xAxes: [{
          barThickness: 40, 
          maxBarThickness: 40,
          barPercentage: 1.0,
          categoryPercentage: 1.0,
          ticks: {
            min: 0,
          },
        }]
      }
    }
  };

  myBarChart = new Chart(ctx, config);

这些是我使用的选项。 给出的是输出的截图

output Image

谁能帮我解决这个问题。 谢谢你

【问题讨论】:

标签: chart.js


【解决方案1】:

删除此barThickness: 40,40 像素)。在您的情况下,这种宽度的“没有空间/房间”=重叠和损坏的布局。

https://www.chartjs.org/docs/latest/charts/bar.html#barthickness

基本sn-p(基于您的代码)(更改barThicknessbarPercentagebarPercentage): https://www.chartjs.org/docs/latest/charts/bar.html#barpercentage-vs-categorypercentage

var canvas = document.getElementById("myChart");
var ctx = canvas.getContext("2d");

var _datesForLabel = ["2020-02-10",
                      "2020-02-13",
                      "2020-02-17",
                      "2020-02-18",
                      "2020-02-19",
                      "2020-02-20",
                      "2020-02-21",
                      "2020-02-22",
                      "2020-02-23",
                      "2020-02-24",
                      "2020-02-25",
                      "2020-02-26",
                      "2020-02-27",
                      "2020-02-28",
                      "2020-02-29",
                      "2020-03-01",
                      "2020-03-02",
                      "2020-03-03",
                      "2020-03-04",
                      "2020-03-05",
                      "2020-03-07",
                      "2020-03-08",
                      "2020-03-09",
                      "2020-03-10","2020-02-10",
                      "2020-02-13",
                      "2020-02-17",
                      "2020-02-18",
                      "2020-02-19",
                      "2020-02-20",
                      "2020-02-21",
                      "2020-02-22",
                      "2020-02-23",
                      "2020-02-24",
                      "2020-02-25",
                      "2020-02-26",
                      "2020-02-27",
                      "2020-02-28",
                      "2020-02-29",
                      "2020-03-01",
                      "2020-03-02",
                      "2020-03-03",
                      "2020-03-04",
                      "2020-03-05",
                      "2020-03-07",
                      "2020-03-08",
                      "2020-03-09",
                      "2020-03-10"]      

var _chartDataWithOptions =[];

_chartDataWithOptions.push({
  label:"dataseries1",
  data:[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],
  backgroundColor:"blue"
})

_chartDataWithOptions.push({
  label:"dataseries2",
  data:[2,3,4,5,6,7,8,9,10,12,13,11,10,19,14,12,11,18,26,23,21,28,24,2,3,4,6,9,1,2,1,11,12,13,14,15,16,17,18,19,20,21,22,23,11,22,4,6,3,6],
  backgroundColor:"red"
})

var config = {
  type: 'bar',
  data: {
    labels: _datesForLabel,
    datasets: _chartDataWithOptions,
    borderSkipped: 'top'
  },
  options: {
    // responsive: true,
    tooltips: {
      // mode: ''
    },
    plugins: {
      colorschemes: {
        scheme: 'office.Waveform6'
      }
    },
    scales: {
      yAxes: [{
        ticks: {
          min: 0,
        }
      }],
      xAxes: [{
        // barThickness: 40, // number (pixels) or 'flex'
        maxBarThickness: 40,
        barPercentage: 1,/* change this */
        categoryPercentage: 0.5,/* change this */
        ticks: {
          min: 0,
        },
      }]
    }
  }
};

myBarChart = new Chart(ctx, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js" ></script>

<div style="height: 500px; width: 100%;">
<canvas id="myChart" ></canvas>
</div>

关于“设置标签而不旋转”-再次“没有空间”-maxRotation: 0,-完整答案+例如她:Chart Js Change Label orientation on x-Axis for Line Charts

“积分/数据过多”问题: 现在“没有办法”自动分组数据 - 一个想法是使用 stacked: true(“节省空间”) - 或手动过滤您的数据(显示更少的点 - 相关 StackOverflow Q:Chartjs 2 scaling lots of data points)。

【讨论】:

  • 感谢您的回复。我必须按照您的建议进行操作,但找不到条形图的确切工作模型。我找到插件 chartjs-plugin-downsample 示例:albinodrought.github.io/chartjs-plugin-downsample/samples/… 但这不适用于柱形图。谢谢。
  • 有没有其他方法可以美化图表,或者我从上面的建议中忘记的任何东西。
  • 再次添加数据示例(代码 sn-p)。另一个想法是这个插件(ZOOM):codepen.io/nikjohn/details/WNbRQRb。我编辑答案(空间之间)。
  • 感谢您的回复。我检查了您建议的链接,我们应该使用它。但是在这里找到的替代解决方案jsfiddle.net/jmpxgufu 没有任何插件。我添加的宽度越大,我拥有的空间就越多,在这个例子中还添加了一个滚动条。我在stackoverflow.com/questions/35854244/… 上找到了这个,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 2014-10-30
  • 2022-08-18
  • 2023-04-04
  • 2016-02-25
相关资源
最近更新 更多