- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 ChartJS 2.4,我想在数据集中预定义很多设置。但是使用一个空数据数组和一个未定义的标签。现在我想阻止 ChartJS 呈现图例,然后显示为“null”或“undefined”。后来我设置了一个合适的标签并开始将数据推送到数据集,最后在图表上调用 update() 效果很好。
这是一个基本的 fiddle使用此代码:
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 5,
pointHitRadius: 10,
data: [],
}
]
};
var myLineChart = new Chart("myChart", {
type: "line",
data: data
});
setTimeout(function(){
data.datasets[0].label = "The Label"
data.datasets[0].data.push(10, 20)
myLineChart.update()
}, 2000)
我试图覆盖 generateLabels 但我没有成功。并且设置 dateaset.hidden 仅在 true 但不是真正“隐藏”时穿过图例。
编辑 1:另一方面,将 options.legend.display
设置为 false 会永远隐藏所有图例,这也不是我需要的。
最佳答案
让我们创建一个插件,在每次更新开始时,根据第一个数据集的标签是否(分别)定义来决定是否显示图例。 autoDisplayLegend
图表选项启用插件,如果设置为 true
。
工作 fiddle 是 here .该代码也可以在下面找到。
var autoDisplayLegendPlugin = {
// Called at start of update.
beforeUpdate: function(chartInstance) {
if (chartInstance.options.autoDisplayLegend) {
// The first (and, assuming, only) dataset.
var dataset = chartInstance.data.datasets[0];
if (dataset.label)
chartInstance.options.legend.display = true;
else
chartInstance.options.legend.display = false;
}
}
};
Chart.pluginService.register(autoDisplayLegendPlugin);
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 5,
pointHitRadius: 10,
data: [],
}]
};
var myLineChart = new Chart("myChart", {
type: "line",
data: data,
options: {
// Option to auto display the legend.
autoDisplayLegend: true
}
});
setTimeout(function() {
data.datasets[0].label = "The Label"
data.datasets[0].data.push(10, 20)
myLineChart.update()
}, 2000)
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.bundle.js"></script>
<canvas id="myChart" width="400" height="250"></canvas>
关于chart.js - 如果标签是假的,ChartJS 不会呈现任何图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41791480/
有没有办法在 vue-chartjs 中制作带圆角的条形图?我在谷歌上搜索了一下,发现我们可以使用此 url 中给出的渲染条的实现来扩展条形图 - How to put rounded corners
我想通过提供两个数组来创建多条垂直线,第一个称为marketing,其中包含诸如“2017-09-21”等日期和一个数组称为 amount,仅包含数字。 我使用 ChartJS 创建了折线图。最终结果
(对不起我的英语)我将 chartjs-plugin-streaming 用于实时 chartjs,我想使用 chartjs-plugin-zoom。 但是缩放不起作用。 当我测试缩放时,出现缩放区域
我使用 angualr2-chartjs 来显示图表。我的问题是当我动态更改选项时图表更新只有一次。在控制台中,我看到一切都很好,但在 View 中我看不到变化。我使用了 chartjs-plugin
我正在尝试创建类似于 www.chartjs.org 顶部的内容其中有一个随机移动的背景条形图。我发现了另一个类似的question ,但是它似乎不起作用。当我在下面添加时,它不会添加除 400x40
感谢任何提示和/或帮助!我在这里碰壁试图让图表呈现。我已经恢复到测试一个非常简单的方法(下面的代码),但我仍然收到以下错误: 类型错误:无法读取未定义的属性(读取“ map ”) 我可以记录从 use
chartjs-plugin-zoom是 Chart.js 的缩放和平移插件 您可以调用 chart.resetZoom() 以编程方式将缩放重置为默认状态。参见 this example on js
我正在尝试将 Chart.js 与 Vue.js 一起使用,这就是我得到的编译结果,但我没有在 GUI 上看到任何显示。 这是我的文件 DonutChart.vue: // NOT SURE IF
好的,所以我正在尝试通过 VueJS 创建一个 ChartJS 实例,以便我可以通过组件内的 AJAX 请求轻松更新其数据。 基本上它可以工作,创建了 ChartJS 实例,但它是空的,高度和宽度为
我想从 chartjs-chart-treemap 添加树状图到我现有的使用 react-chartjs-2 的项目中. 我到目前为止尝试过: import ChartComponent from '
是否可以定义具有多级别/类别轴的条形图? 例如,我想显示地区/省类别,如下 Excel 图表所示: 我找到了this使用多个 xAxes 的示例。 xAxes:[ { id:'xA
我正在使用 chart.js (V2) 尝试构建一个条形图,用户无需将鼠标悬停在任何地方或点击任何地方即可获得更多信息。我提供了两个示例,说明我希望如何编辑我的图表。 Two edited versi
我想使用 Chartjs(chartjs.org) 作为我的图表工具以及使用 TypeScript 的 AngularJS。我已经从 GitHub 安装了 DefinitelyTyped for Ch
我正在使用 chartjs-plugin-datalabels,并且在大图表中显示较小的数据集时值重叠 这是 chartjs-data-plugin 配置 options: { plu
var yLabels = { 1 : 'New', 2 : 'Learning Phase', 3
我正在创建图表以使用图表 js 呈现一些数据。 const data = { labels: ["1 Dec", "8 Dec", "16 Dec", "31 Dec"], dat
我正在尝试将其他数据插入圆环图中。 Controller 将这样的数组传递给 View : [ 0 => array:3 [ "profit" => 20 "sex" => arr
将百分比值添加到图例标签的最简单方法是什么? 我相信它会使用 generateLabels: function (chart) {},谁能提供一个干净的例子? 最佳答案 似乎没有任何本地方式来显示百分
我已经多次看到这个问题,但我找不到适合我的解决方案。 我将一个 Django 变量传递给 Chartjs 进行绘图,所有的个位数都是正确的,但它将两位数变成了一个。像 11 是 1,1...23 是
我正在尝试使用 chartjs 数据标签插件获取每个条上的值。 所以在条形“a”上方 a 想看到数字 30 及以上或在条形“b”内我想看到数字 50。 但它根本没有显示任何值。 任何人都可以帮助并告诉
我是一名优秀的程序员,十分优秀!