gpt4 book ai didi

javascript - 如何在 Plottable.js 直方图中启用图例

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:34 25 4
gpt4 key购买 nike

我有以下脚本。它只是绘制直方图在给定的数据中。

"use strict";

var xScale = new Plottable.Scales.Category();
var yScale = new Plottable.Scales.Linear();
var colorScale = new Plottable.Scales.Color();



var primaryData = [{ x: 1, y: 1 }, { x: 2, y: 3 }, { x: 3, y: 2 }, { x: 4, y: 4 }, { x: 5, y: 3 }, { x: 6, y: 5 }];
var secondaryData = [{ x: 1, y: 2 }, { x: 2, y: 1 }, { x: 3, y: 2 }, { x: 4, y: 1 }, { x: 5, y: 2 }, { x: 6, y: 1 }];

var plot = new Plottable.Plots.ClusteredBar().addDataset(new Plottable.Dataset(primaryData).metadata(5)).addDataset(new Plottable.Dataset(secondaryData).metadata(3)).x(function (d) {
return d.x;
}, xScale).y(function (d) {
return d.y;
}, yScale).attr("fill", function (d, i, dataset) {
return dataset.metadata();
}, colorScale).renderTo("svg#example");


// Why this legend does not appear.
var legend = new Plottable.Components.Legend(colorScale);
legend.xAlignment("center");
legend.yAlignment("center");
legend.renderTo("svg#legend_example");
<link href="http://plottablejs.org/assets/css/application.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/plottable.js/1.12.0/plottable.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/plottable.js/1.12.0/plottable.css" rel="stylesheet"/>

<html>
<body>
<div class="component__svg">
<svg width="100%" height="100%" id="example"></svg>

<svg width="100%" height="100%" id="legend_example"></svg>
</div>
</body>
</html>

如果你执行代码,你可以看到图例没有出现。如何启用它并将其放在图的右上角?

最佳答案

任何基于 Plottable 的图表的核心都是基于表格的布局引擎。您的脚本中有两个组件,即情节和图例。您可以使用 Plottable.Components.Table 并将表表示为像这样的组件数组的数组

var chart = new Plottable.Components.Table([[plot, legend]]);

并将其渲染为一个 svg 元素

chart.renderTo("svg#example");

这是您的代码的 JSFiddle 链接:http://jsfiddle.net/76j3wtoc/

关于javascript - 如何在 Plottable.js 直方图中启用图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32757725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com