首页 > 解决方案 > ChartJS 每个数据集/独立数据集的单独标签?

问题描述

我实际上是在尝试创建一个包含 2-8 个项目的条形图,其中底部/图例上的标签是短产品代码(例如:4380),鼠标悬停在条形图上会显示完整的 SKU/产品名称。

我已经让它大部分工作了,但我的实现是两种不受欢迎的方式之一。

  1. 数据点全部组合成第一个产品编号/图表标签。

所有数据集结合

  1. 空白点使条形变小/不填满整个宽度。

酒吧太小

我渲染图表的代码如下:

var myBarChart2;
$.ajax({
    url: "chartdata.php",
    data: { 
        "skugroup": group
    },
    method: 'GET',
    dataType: 'json',
    success: function (d) {
        Chart.defaults.global.defaultFontFamily = '-apple-system,system-ui,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif';
        Chart.defaults.global.defaultFontColor = '#292b2c';
        
        var ctx = document.getElementById("inventorybarchart");
        myBarChart2 = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: d.labels,
                datasets: d.datasets,
            },
            options: {
                scales: {
                    xAxes: [{
                        gridLines: {
                          display: false
                        },
                        ticks: {
                            display: true
                        }
                    }],
                    yAxes: [{
                        ticks: {
                            min: 0,
                            beginAtZero: true
                        },
                        gridLines: {
                          display: true
                        }
                    }],
                },
                legend: {
                  display: false
                }
            }
        });
    }
}); 

两个版本的ajax响应如下:

版本 1:

{"datasets":[{"labels":"GRAY-DARK-GRAY","backgroundColor":"rgba(164,222,164,1)","borderColor":"rgba(164,222,164,1)","data":[5996]},{"labels":"CANARY-YELLOW","backgroundColor":"rgba(35,148,58,1)","borderColor":"rgba(35,148,58,1)","data":[4605]},{"labels":"PINK-WHITE-GRAY","backgroundColor":"rgba(101,24,125,1)","borderColor":"rgba(101,24,125,1)","data":[1288]},{"labels":"SEAFOAM-WHITE-GRAY","backgroundColor":"rgba(129,74,64,1)","borderColor":"rgba(129,74,64,1)","data":[3463]},{"labels":"YELLOW-WHITE-GRAY","backgroundColor":"rgba(91,216,70,1)","borderColor":"rgba(91,216,70,1)","data":[1537]},{"labels":"WHITE-YELLOW","backgroundColor":"rgba(101,225,237,1)","borderColor":"rgba(101,225,237,1)","data":[152]}],"labels":["4380","4311","4571","4588","4557","4373"]}

版本 2:

{"datasets":[{"label":"GRAY-DARK-GRAY","backgroundColor":"rgba(1,1,235,1)","borderColor":"rgba(1,1,235,1)","data":[5996,null,null,null,null]},{"label":"CANARY-YELLOW","backgroundColor":"rgba(12,87,184,1)","borderColor":"rgba(12,87,184,1)","data":[null,4605,null,null,null]},{"label":"PINK-WHITE-GRAY","backgroundColor":"rgba(85,107,126,1)","borderColor":"rgba(85,107,126,1)","data":[null,null,1288,null,null]},{"label":"SEAFOAM-WHITE-GRAY","backgroundColor":"rgba(181,150,65,1)","borderColor":"rgba(181,150,65,1)","data":[null,null,null,3463,null]},{"label":"YELLOW-WHITE-GRAY","backgroundColor":"rgba(132,66,28,1)","borderColor":"rgba(132,66,28,1)","data":[null,null,null,null,1537]},{"label":"WHITE-YELLOW","backgroundColor":"rgba(49,195,217,1)","borderColor":"rgba(49,195,217,1)","data":[null,null,null,null,null]}],"labels":["4380","4311","4571","4588","4557","4373"]}

唯一的区别是我总是对 datasets[index].data 使用 0 索引,或者我根据它应该在哪里填写 null。

我应该改变图表的呈现方式还是应该改变数据的传递方式?

作为记录,鼠标悬停显示正确的 sku/全名。

标签: javascriptchart.js

解决方案


我会在一个单独的属性中定义数据dataset并将完整的产品名称保存在一个单独的属性中。

const data = {
  "labels": ["4380", "4311", "4571", "4588", "4557", "4373"],
  "productNames": ["GRAY-DARK-GRAY", "CANARY-YELLOW", "PINK-WHITE-GRAY", "SEAFOAM-WHITE-GRAY", "YELLOW-WHITE-GRAY", "WHITE-YELLOW"],
  "datasets": [{
    "data": [5996, 4605, 1288, 3463, 1537, 152],
    ...
  }]
};

要在 中显示产品名称tooltip,您必须定义一个标签回调函数,如下所示:

tooltips: {
  callbacks: {
    label: (tooltipItem, data) => {
      let i = tooltipItem.index;
      return data.productNames[i] + ': ' + data.datasets[0].data[i];
    }
  }
}

请查看您修改后的代码,看看它是如何工作的。

const data = {
  "labels": ["4380", "4311", "4571", "4588", "4557", "4373"],
  "productNames": ["GRAY-DARK-GRAY", "CANARY-YELLOW", "PINK-WHITE-GRAY", "SEAFOAM-WHITE-GRAY", "YELLOW-WHITE-GRAY", "WHITE-YELLOW"],
  "datasets": [{
    "data": [5996, 4605, 1288, 3463, 1537, 152],
    "backgroundColor": ["rgba(1,1,235,1)", "rgba(12,87,184,1)", "rgba(85,107,126,1)", "rgba(181,150,65,1)", "rgba(132,66,28,1)", "rgba(49,195,217,1)"],
    "borderColor": ["rgba(1,1,235,1)", "rgba(12,87,184,1)", "rgba(85,107,126,1)", "rgba(181,150,65,1)", "rgba(132,66,28,1)", "rgba(49,195,217,1)"]
  }]
};

var ctx = document.getElementById("inventorybarchart");
myBarChart2 = new Chart(ctx, {
  type: 'bar',
  data: data,
  options: {
    scales: {
      xAxes: [{
        gridLines: {
          display: false
        }
      }],
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }],
    },
    legend: {
      display: false
    },
    tooltips: {
      callbacks: {
        label: (tooltipItem, data) => {
          let i = tooltipItem.index;
          return data.productNames[i] + ': ' + data.datasets[0].data[i];
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.0/Chart.min.js"></script>
<canvas id="inventorybarchart" height="90"></canvas>


推荐阅读