质谱图控制:在图表区域绘图和贴标线系列

问题描述:

我对MS ASP.NET图表控件有疑问。质谱图控制:在图表区域绘图和贴标线系列

  1. 线条系列如何设置在条形系列上,以便它延伸到图表的y轴?
  2. 是否可以将图表系列的名称(即“目标”)放置在图表的右侧以替代将此系列添加到图例中?

正如您在下面的屏幕截图中看到的,我有一个线条系列展示在没有延伸到图表y轴的条形系列之上。

enter image description here

的代码如下:

var data1 = new Dictionary<string, float> 
{ 
    { "W1", 80}, 
    { "W2", 60}, 
    { "W3", 40}, 
    { "W4", 20}, 
    { "W5", 10} 
}; 
var data2 = new Dictionary<string, float> 
{ 
    { "W1", 10}, 
    { "W2", 10}, 
    { "W3", 0}, 
    { "W4", 10}, 
    { "W5", 10} 
}; 
var data3 = new Dictionary<string, float> 
{ 
    { "W1", 10}, 
    { "W2", 30}, 
    { "W3", 50}, 
    { "W4", 70}, 
    { "W5", 80} 
}; 
var data4 = new Dictionary<string, float> 
{ 
    { "W1", 50}, 
    { "W2", 50}, 
    { "W3", 50}, 
    { "W4", 50}, 
    { "W5", 50} 
}; 

var chart = new Chart(); 
chart.Height = Unit.Pixel(300); 
chart.Width = Unit.Pixel(450); 
chart.Legends.Add("Legend").Alignment = StringAlignment.Center; 
chart.Palette = ChartColorPalette.None; 
chart.PaletteCustomColors = new Color[] { Color.FromArgb(191, 214, 151), Color.FromArgb(249, 255, 149), Color.FromArgb(191, 79, 75), Color.Green }; 

var area = new ChartArea(); 
area.AxisX.MajorGrid.LineColor = Color.Transparent; 
chart.ChartAreas.Add(area); 

var series1 = new Series("Done"); 
foreach (var item in data1) 
{ 
    series1.Points.AddXY(item.Key, item.Value); 
} 
series1.MarkerBorderWidth = 1; 

var series2 = new Series("In Progress"); 
foreach (var item in data2) 
{ 
    series2.Points.AddXY(item.Key, item.Value); 
} 

var series3 = new Series("Needs Review"); 
foreach (var item in data3) 
{ 
    series3.Points.AddXY(item.Key, item.Value); 
} 

var series4 = new Series("Goal"); 
foreach (var item in data4) 
{ 
    series4.Points.AddXY(item.Key, item.Value); 
} 
series4.ChartType = SeriesChartType.Line; 
series4.BorderWidth = 2; 

series1.ChartType = series2.ChartType = series3.ChartType = SeriesChartType.StackedColumn; 
series1.Font = series2.Font = series3.Font = series4.Font = new Font("Verdana", 8.25f, FontStyle.Regular); 

chart.Series.Add(series1); 
chart.Series.Add(series2); 
chart.Series.Add(series3); 
chart.Series.Add(series4); 

感谢您的帮助。


UPDATE:
正如我继续寻找合适的解决方案,我实现了一个额外的图表只是“目标”行系列的意图:

  • 设置的某些颜色这个新的图表的特性的透明
  • 特此图表上的现有的图表
012的顶

该方法通过在杆系列上显示“Goal”线系列并允许“Goal”线系列延伸至y轴来提供正确的显示。但它禁用了现有图表上条形图系列的工具提示和点击动作。由于这种用户体验不足,这种方法不是一个合适的解决方案。

一个解决方案继续搜索...

问题1:

另一种方法是使用PostPaint事件,在那里你可以画任何东西,你想,在图表上的任何地方......但仍然会失去工具提示和该行的这种交互功能。

对于你的问题2:

您可以排除从图例项集合不需要的图例项;就像是做这个例子中,你可以自定义的传说:

yourChart.CustomizeLegend += new EventHandler<CustomizeLegendEventArgs> (CustomizeLegendEventHandler); 
//... 
static void CustomizeLegendEventHandler(object sender, CustomizeLegendEventArgs e) 
{ 
    int anotherIndex = 3; 
    if (sender != null && sender is Chart) 
    { 
    if (e != null && e.LegendItems != null && e.LegendItems.Count > 0) 
    { 
     Chart ch = ((Chart)sender); 
     if (...) //your logic here 
     { 
     //example: you can move a legend item from one index to another here: 
     LegendItem item = e.LegendItems[0]; 
     e.LegendItems.RemoveAt(0); 
     e.LegendItems.Insert(anotherIndex, item); 
     } 
    } 
    } 
} 

而且,你可以有一个次级与Y轴只有一个标签,一个点等,或:你可以使用PostPaint事件处理程序绘制任何你希望。

您可以从MS下载一个很好的安装示例包:here。他们展示了许多您可以学习的代码示例。我从这些样品中发现了这些东西,并使用了这种新的*反射器:ILSpy

希望这会有所帮助。

+0

回答:您对问题2的解决方案将使用PostPaint事件处理程序绘制任何我希望也失去图表的工具提示和交互功能的东西?假设有一个第二Y轴不会导致这种不希望的用户体验是否公平?谢谢您的帮助。 – phreeskier

+0

@phreeskier:我提供了这种方法来绘制或使用第二个轴上的空白数据点,因为您不想在图例中使用label.entry。如果你使用一个空白点的系列,你会有交互性 - 如果你画它 - 你将不会有交互性。 我认为可以安全地假设由于第二个Y轴而不会对用户体验产生负面影响。 – woohoo