Merged charts are appearing one over another. I want them side by side.

huangapple go评论84阅读模式
英文:

merged charts are appearing one over another. I want them side by side

问题

以下是代码部分的翻译:

我想要两个图表:饼图和面积百分比图放在同一个SVG中。饼图将位于左侧50%,面积图将位于右侧50%。

但在下面的代码中,饼图出现在面积图上面。我做错了什么?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<head>
    <title>Highcharts - 组合图表</title>
    <!-- 包含 Highcharts 库 -->
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <!-- 包含 Highcharts 导出模块 -->
    <script src="https://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
    <!-- 用于组合图表的容器 -->
    <div id="combinedChartContainer" style="width: 100%; height: 400px; display: flex;">
        <div id="pieChartContainer" style="flex: 1; padding: 10px;"></div>
        <div id="areaChartContainer" style="flex: 1; padding: 10px;"></div>
    </div>

    <script>
        // 饼图数据
        var pieData = [{
            name: '类别 1',
            y: 45
        }, {
            name: '类别 2',
            y: 55
        }];

        // 面积百分比图数据
        var areaPercentageData = [{
            name: '类别 1',
            data: [20, 30, 40],
            stack: '百分比'
        }, {
            name: '类别 2',
            data: [30, 40, 50],
            stack: '百分比'
        }];

        // 创建组合图表
        var combinedChart = Highcharts.chart('combinedChartContainer', {
            chart: {
                type: 'area'
            },
            title: {
                text: '组合图表'
            },
            xAxis: {
                categories: ['1月1日', '1月2日', '1月3日']
            },
            yAxis: [{
                title: {
                    text: '百分比'
                },
                max: 100,
                stackLabels: {
                    enabled: true,
                    format: '{total}%'
                }
            }, {
                title: {
                    text: '值'
                },
                opposite: true
            }],
            plotOptions: {
                area: {
                    stacking: '百分比',
                    lineColor: '#ffffff',
                    lineWidth: 1,
                    marker: {
                        lineWidth: 1,
                        lineColor: '#ffffff'
                    }
                }
            },
            series: [{
                type: 'area',
                name: '类别 1',
                data: areaPercentageData[0].data,
                stack: '百分比',
                renderTo: 'areaChartContainer' // 指定面积图表容器 div
            }, {
                type: 'area',
                name: '类别 2',
                data: areaPercentageData[1].data,
                stack: '百分比',
                renderTo: 'areaChartContainer' // 指定面积图表容器 div
            }, {
                type: 'pie',
                name: '类别',
                data: pieData,
                size: 150,
                showInLegend: false,
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f}%'
                },
                renderTo: 'pieChartContainer' // 指定饼图容器 div
            }],
            exporting: {
                enabled: true  // 启用导出
            }
        });
    </script>
</body>
</html>

<!-- end snippet -->

希望这可以帮助您理解代码的含义。如果您有任何进一步的问题,请随时提出。

英文:

I want two charts: pie chart and area percentage chart in same svg. Pie chart will be in left 50% and area chart will be in right 50%.

But in following code: pie chart appears over area chart. what am i doing wrong?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Highcharts - Combined Chart&lt;/title&gt;
&lt;!-- Include Highcharts library --&gt;
&lt;script src=&quot;https://code.highcharts.com/highcharts.js&quot;&gt;&lt;/script&gt;
&lt;!-- Include Highcharts exporting module --&gt;
&lt;script src=&quot;https://code.highcharts.com/modules/exporting.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!-- Container for the combined chart --&gt;
&lt;div id=&quot;combinedChartContainer&quot; style=&quot;width: 100%; height: 400px; display: flex;&quot;&gt;
&lt;div id=&quot;pieChartContainer&quot; style=&quot;flex: 1; padding: 10px;&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;areaChartContainer&quot; style=&quot;flex: 1; padding: 10px;&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
// Pie Chart Data
var pieData = [{
name: &#39;Category 1&#39;,
y: 45
}, {
name: &#39;Category 2&#39;,
y: 55
}];
// Area Percentage Chart Data
var areaPercentageData = [{
name: &#39;Category 1&#39;,
data: [20, 30, 40],
stack: &#39;percentage&#39;
}, {
name: &#39;Category 2&#39;,
data: [30, 40, 50],
stack: &#39;percentage&#39;
}];
// Create the combined chart
var combinedChart = Highcharts.chart(&#39;combinedChartContainer&#39;, {
chart: {
type: &#39;area&#39;
},
title: {
text: &#39;Combined Chart&#39;
},
xAxis: {
categories: [&#39;1-Jan&#39;, &#39;2-Jan&#39;, &#39;3-Jan&#39;]
},
yAxis: [{
title: {
text: &#39;Percentage&#39;
},
max: 100,
stackLabels: {
enabled: true,
format: &#39;{total}%&#39;
}
}, {
title: {
text: &#39;Value&#39;
},
opposite: true
}],
plotOptions: {
area: {
stacking: &#39;percentage&#39;,
lineColor: &#39;#ffffff&#39;,
lineWidth: 1,
marker: {
lineWidth: 1,
lineColor: &#39;#ffffff&#39;
}
}
},
series: [{
type: &#39;area&#39;,
name: &#39;Category 1&#39;,
data: areaPercentageData[0].data,
stack: &#39;percentage&#39;,
renderTo: &#39;areaChartContainer&#39; // specify the area chart container div
}, {
type: &#39;area&#39;,
name: &#39;Category 2&#39;,
data: areaPercentageData[1].data,
stack: &#39;percentage&#39;,
renderTo: &#39;areaChartContainer&#39; // specify the area chart container div
}, {
type: &#39;pie&#39;,
name: &#39;Categories&#39;,
data: pieData,
size: 150,
showInLegend: false,
dataLabels: {
enabled: true,
format: &#39;&lt;b&gt;{point.name}&lt;/b&gt;: {point.percentage:.1f}%&#39;
},
renderTo: &#39;pieChartContainer&#39; // specify the pie chart container div
}],
exporting: {
enabled: true  // Enable exporting
}
});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

Screenshot of problem:
Merged charts are appearing one over another. I want them side by side.

答案1

得分: 1

以下是翻译好的部分:

这是正常行为,现在您需要定位系列。以下是示例配置:

Highcharts.chart('container', {
  chart: {
    marginLeft: 0
  },
  yAxis: [{
    left: '50%',
    width: '50%'
  }],

  xAxis: [{
    left: '50%',
    width: '50%'
  }],

  series: [{
    type: 'areaspline',
    data: [1, 2, 3, 4]
  }, {
    type: 'pie',
    center: ['20%', '50%'],
    data: [1, 2, 3, 4]
  }]
});

演示链接:
https://jsfiddle.net/BlackLabel/kdr6cz08/

英文:

This is the normal behavior and now you need to position series. Here is the example config:

Highcharts.chart(&#39;container&#39;, {
chart: {
marginLeft: 0
},
yAxis: [{
left: &#39;50%&#39;,
width: &#39;50%&#39;
}],
xAxis: [{
left: &#39;50%&#39;,
width: &#39;50%&#39;
}],
series: [{
type: &#39;areaspline&#39;,
data: [1, 2, 3, 4]
}, {
type: &#39;pie&#39;,
center: [&#39;20%&#39;, &#39;50%&#39;],
data: [1, 2, 3, 4]
}]
});

Demo:
https://jsfiddle.net/BlackLabel/kdr6cz08/

huangapple
  • 本文由 发表于 2023年4月17日 02:08:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定