gpt4 book ai didi

javascript - Chartist .SVG 对齐/填充

转载 作者:行者123 更新时间:2023-11-28 14:19:49 26 4
gpt4 key购买 nike

我使用以下数据生成 Chartist 饼图:

var mapPieData = {
series: [
{ value: 578, className: "pieNegativeColour", label: "online" },
{ value: 3182, className: "piePositiveColour", label: "offline" }
],
highest: { label: "Huawei", value: "10258", className: "pieColour1" },
maximum: 3760};

我使用以下选项来配置它:

var mapPieOptions = {
showLabel: true,
fullWidth: true,
chartPadding: 0};

我必须在 .SVG map 上叠加生成的饼图。

问题是生成的饼图位于一个 .SVG 容器的中心,该容器比它需要的更宽。这意味着定位是不切实际的。如果我将饼图放在左上角,它实际上会在顶部中间结束,这不是我想要的。

enter image description here

我想删除饼图周围的多余空间。

最佳答案

我能够使用您在网站上提供的代码以及示例 fiddle 重新创建您的饼图。

http://gionkunz.github.io/chartist-js/examples.html

$('.ct-chart').css({'background-color':'white'});

var data = {
series: [
{ value: 578, className: "ct-series-c", label: "online" },
{ value: 3182, className: "ct-series-a", label: "offline" }
],
highest: { label: "Huawei", value: "10258", className: "pieColour1" },
maximum: 3760
};

var options = {
showLabel: true,
fullWidth: true,
chartPadding: 0
};

new Chartist.Pie('.ct-chart', data, options);

pie chart smack in the middle

我对此进行了分析并注意到,如果我将填充设置为负值,渲染的大小会增加,但会被裁剪。

var options = {
showLabel: true,
fullWidth: true,
chartPadding: -40
};

clipped chart

然后我增加了包含元素的大小,它位于 100%,但实际上并没有占据整个高度。

通过将容器元素设置为 750px 高度(与其宽度一样宽,它将占据元素的整个宽度。

750 px

broad as it is wide chart

所以现在我们必须将其自动化。假设您手头有 jQuery,您可以简单地执行以下操作:

var $chart = $('.ct-chart');
$chart.css({'height':$chart.width()+'px'});

在片段站点上运行示例:

var $chart = $('.ct-chart');
$chart.css({'background-color':'white','height':$chart.width()+'px'});
var data = {
series: [
{ value: 578, className: "ct-series-c", label: "online" },
{ value: 3182, className: "ct-series-a", label: "offline" }
],
highest: { label: "Huawei", value: "10258", className: "pieColour1" },
maximum: 3760
};

var options = {
showLabel: true,
fullWidth: true,
chartPadding: 0
};

new Chartist.Pie('.ct-chart', data, options);

如果您没有 jQuery,请将 jQuery 行替换为:

var chart = document.querySelector('#pie-with-custom-labels .ct-chart');
chart.style.height = chart.clientWidth+"px";

关于javascript - Chartist .SVG 对齐/填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55374893/

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