asp.net 图表:图例与 X 轴重叠

发布于 2024-10-09 21:58:36 字数 794 浏览 0 评论 0原文

我正在以编程方式创建一个图表(DataVisualization.Charting.Chart),它是一个堆积条形图。

我还以编程方式向其中添加图例条目。我想在图表底部显示图例。

但是,这样做时,图例与图表的 X 轴重叠。

这是我正在使用的代码:

Private Function GetLegend(ByVal legendName As String, ByVal s As Single) As     System.Windows.Forms.DataVisualization.Charting.Legend

 Dim objLegend As System.Windows.Forms.DataVisualization.Charting.Legend = New System.Windows.Forms.DataVisualization.Charting.Legend()

 objLegend.Name = legendName
 objLegend.Font = New System.Drawing.Font("Verdana", s)
 objLegend.IsDockedInsideChartArea = False
 objLegend.Docking = Docking.Bottom
 Return objLegend
End Function

下面的语句将图例添加到图表中

_msChart.Legends.Add(GetLegend("SomeValue1", 10.0F))

任何想法,缺少什么?我只想在底部显示图例,但它不应该与 X 轴重叠。

I am creating a Chart (DataVisualization.Charting.Chart) programmatically, which is a Stacked Bar chart.

I am also adding Legend entries programmatically to it. I want to show the Legend at the bottom of the chart.

But, while doing so, the Legend overlapps with the X-axis of the chart.

Here is the code I am using:

Private Function GetLegend(ByVal legendName As String, ByVal s As Single) As     System.Windows.Forms.DataVisualization.Charting.Legend

 Dim objLegend As System.Windows.Forms.DataVisualization.Charting.Legend = New System.Windows.Forms.DataVisualization.Charting.Legend()

 objLegend.Name = legendName
 objLegend.Font = New System.Drawing.Font("Verdana", s)
 objLegend.IsDockedInsideChartArea = False
 objLegend.Docking = Docking.Bottom
 Return objLegend
End Function

Below statement adds that Legend to the chart

_msChart.Legends.Add(GetLegend("SomeValue1", 10.0F))

Any idea, what is missing? I want to show the legend at the bottom only, but it should not overlapp with the X-axis.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩耳倾听 2024-10-16 21:58:36

我今天也遇到了同样的问题。尝试添加:

objLegend.Position.Auto = true
objLegend.DockedToChartArea = "yourChartAreaName"

这对我没有帮助,但我在网上发现这可能会有所帮助(并且是干净的解决方案)。

对我来说真正有用的是移动图表区域来为图例腾出空间,这样它就不再重叠。我的图例位于顶部,因此这段代码对我有用:

chart.ChartAreas[0].Position.Y = 15

您可以尝试调整它的大小,例如强制它比 chart.Size 短 20 像素。

希望这有帮助。

I had the same problem today. Try adding:

objLegend.Position.Auto = true
objLegend.DockedToChartArea = "yourChartAreaName"

That did not help me but I found on the net that this might be helpful (and clean solution).

What actually worked for me was moving chart area to make space for legend so it no longer overlaps. My legend was on top so this code worked for me:

chart.ChartAreas[0].Position.Y = 15

You can try resizing it instead, forcing it to be for example 20 pixels shorter than chart.Size.

Hope this helps.

所有深爱都是秘密 2024-10-16 21:58:36

我也遇到了重叠的图例/图表区域问题,但这里的其他建议似乎没有任何区别。我认为问题源于图例文本换行为两行,而大小调整算法没有考虑到这一点。

这里的想法让我更清楚地思考这个问题,并且我能够使用以下内容控制图表区域的大小和位置。

Chart1.ChartAreas[0].InnerPlotPosition = new ElementPosition(15, 5, 90, 75);

这些参数没有太多智能感知,但据我推断,这些参数都是总图表区域的百分比(我最初认为它们可能是像素值,并得到了一些非常奇怪的结果)。因此,我上面写的内容会将绘图区域设置为从图表图像左边缘 15% 处开始,从顶部向下 5% 处开始,宽度为 90%,高度为 75%。

I had an overlapping legend/chart area problem as well but none of the other suggestions here seemed to make any difference. I think the problem stems from legend text wrapping to two lines and the sizing algorithms not taking account of this.

The ideas here got me thinking more clearly about the problem though, and I was able control the size and position of the chart area using the following.

Chart1.ChartAreas[0].InnerPlotPosition = new ElementPosition(15, 5, 90, 75);

There's not much intellisense on those parameters, but as well as I could deduce, the parameters are all percentages of the total chart area (I initially thought they might be pixel values and got some very odd results). So what I've written above would set the plot area to start at 15% in from the left edge of the chart image and 5% down from the top, and have a width of 90% and a height of 75%.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文