【发布时间】:2016-07-05 02:42:28
【问题描述】:
目前我需要开发一个以图表为主要组件的工具。图表控件对我来说也是新的。我做了大量的阅读、研究来学习和理解图表控件的全貌。
毕竟,我一直在思考如何在堆积柱形图上绘制水平线(蓝色和红色水平线),如下图所示:
这是我到目前为止所做的:
这是我目前的代码:
// X-Axis labels settings
chart.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
chart.ChartAreas[0].AxisX.Interval = 1;
// Y-Axis labels settings
//chart.ChartAreas[0].AxisY.Minimum = 100;
chart.ChartAreas[0].AxisY.Minimum = 95;
// Plotting chart
using (YieldEntities context = new YieldEntities())
{
// Extract yield loss list
var yeilds = (
from yeild in context.YeildDatas
group yeild by new { yeild.Loss } into newyeild
select new
{
Loss = newyeild.Key.Loss,
Percentage = newyeild.Sum(p => p.Percentage)
}).OrderByDescending(p => p.Percentage);
//context.YeildDatas.Select(p => new { p.Loss, Percentage = p }).Distinct();
// Create new series
foreach (var yield in yeilds)
{
chart.Series.Add(yield.Loss);
chart.Series[yield.Loss].ChartType = SeriesChartType.StackedColumn100;
}
// Label settings for first series
chart.Series[0].SmartLabelStyle.Enabled = false;
chart.Series[0].LabelAngle = -90;
chart.Series[0].Font = new Font(Font.FontFamily, 15, FontStyle.Bold);
chart.Series[0].IsValueShownAsLabel = true;
var query = context.YeildDatas.ToList();
foreach (var item in query)
{
DataPoint dp = new DataPoint();
dp.SetValueXY(item.DateString, item.Percentage);
chart.Series[item.Loss].Points.Add(dp);
}
// Set empty datapoint for each series
foreach (var yield in yeilds)
{
DataPoint nulldp = new DataPoint();
nulldp.SetValueXY("", 0);
chart.Series[yield.Loss].Points.Insert(1, nulldp);
chart.Series[yield.Loss].Points.Insert(6, nulldp);
chart.Series[yield.Loss].Points.Insert(11, nulldp);
}
chart.Legends["Legend"].IsEquallySpacedItems = true;
chart.Legends["Legend"].IsTextAutoFit = true;
}
希望有高手指导我解决这个问题。
【问题讨论】:
-
您自己绘制图表控件吗?或者你正在使用成品图表控件?如果您使用的是完成图表控件,请尝试在图表控件上使用事件 Paint 并尝试在那里绘制线条
-
我在 Visual Studio 2015 中使用提供的图表控件。先生,如何画线?
-
你的问题本质上是一组需求。有什么代码可以展示吗?
-
我只需要在每个堆叠列的顶部绘制水平线/标记/目标(红色和蓝色线)。
-
除了实际绘图之外,您还可以考虑添加 LineAnnotations 或添加一系列 Charttype.Line 以获得相同的效果。