我在Visual Studio 2013中使用Chart类来可视化我的一些数据。但是,我的数据很快产生了许多系列,将它们全部放在一个图表中非常重要。我将图例区域限制在整个图表区域的20%,因此当我将图表拉伸到最大尺寸时,我几乎无法显示超过7-8个图例项目。控件只是在它用完图例项目的空间后才放......
而不仅仅是写...,是否有可能在图例中添加滚动条并能够看到所有项目?我知道我可以通过某种方式实现自己的传奇,但我想从图表类提供的功能中获得最大的收益。我还想在每个图例项旁边添加复选框,以指示系列是否应该隐藏在图表上。没有我自己的传奇实现可以做到这一点吗?
此外,我还希望通过右键单击带有几个选项的图例项目来展开菜单,但这是完全可选的。滚动条和复选框现在是我的主要问题。
感谢。
答案 0 :(得分:3)
总体思路:您必须创建两个图表。一个是主要的,仅为传奇的第二个。如果系列订单相同,您将拥有相同的系列样式。
右键单击图例项目时显示弹出窗口:
将ContextMenu(工具箱中的ContextMenuStrip类)连接到图例的图表。
用于显示图例隐藏系列:
您必须实现MouseClick
事件处理程序并使用数学检查鼠标光标下的对象(GetChildAtPoint()
方法对图例项目不起作用)。公式:是series_index = control_relative_mouse_y / c_legendItemHeight
,其中c_legendItemHeight
是您为计算控件高度(单个图例项的高度)提供的值。
您必须将图例图表配置为包含LegendStyle
至Row
,MaximumAutoSize
至100
,Docking
至Left
,IsTextAutoFit
到false
和IsEquallySpacedItems
到true
。
您已在图例中定义了3列(一个用于系列样式,第二个用于复选框,第三个用于系列名称)。使用系列CustomProperties
来保持可见状态。在检查列中,使用此自定义属性(Text = "#CUSTOMPROPERTY(...)"
)来显示检查状态。图表不支持自动调整大小。你可以手动完成。在系列加载期间,将图表高度设置为计算值。该值等于_stock.Shares.Count * c_legendItemHeight + 9
。其中:_stock.Shares.Count
是图例中的项目数,c_legendItemHeight
项目的常量高度(整数值,数字更大,然后18似乎对我有效),9
(似乎是常量)。我知道这不好,但我找不到更好的解决方案。我在我的例子中添加了502系列,它工作正常。确保图表中没有任何边距,否则您将无法正确计算序列号。
对于“传奇中的许多系列”问题:
将您的图例图表放入打开AutoScroll属性的面板中。使用上面描述中的表达式设置面板和图例高度。
源代码:
public partial class Form1 : Form
{
private const int c_legendItemHeight = 20;
private const string c_checkCustomPropertyName = "CHECK";
private const string c_checkedString = "✔"; // see http://www.edlazorvfx.com/ysu/html/ascii.html for more
private const string c_uncheckedString = "✘";
private Stock _stock;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_stock = Stock.Load();
// mainChart
mainChart.Legends.Clear();
foreach (Share share in _stock.Shares)
{
Series series = mainChart.Series.Add(share.Name);
series.ChartType = SeriesChartType.Line;
foreach (ShareQuotation shareQuotation in share.Quotations)
{
series.Points.AddXY(shareQuotation.Date.ToString(), shareQuotation.Close);
}
}
// LegendChart
Legend legend = legendChart.Legends[0];
legendChart.Series.Clear();
legend.IsTextAutoFit = false;
legend.IsEquallySpacedItems = true;
legend.MaximumAutoSize = 100;
legend.Docking = Docking.Left;
legend.LegendStyle = LegendStyle.Column;
legend.Position.Auto = true;
legend.Position.Width = 100;
legend.Position.Height = 100;
legend.CellColumns[1].Text = "#CUSTOMPROPERTY(" +c_checkCustomPropertyName+ ")";
foreach (Share share in _stock.Shares)
{
Series series = legendChart.Series.Add(share.Name);
series.SetCustomProperty(c_checkCustomPropertyName,c_checkedString);
}
legendChart.Height = _stock.Shares.Count * c_legendItemHeight + 9; // 9 - seems to be constant value
legendPanel.Height = legendChart.Height;
}
private void legendChart_MouseClick(object sender, MouseEventArgs e)
{
Point mousePosition = legendChart.PointToClient(Control.MousePosition);
int seriesNo = mousePosition.Y / c_legendItemHeight;
Series series = legendChart.Series[seriesNo]; // TODO - check if not out of range
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
// check uncheck series
if (series.GetCustomProperty(c_checkCustomPropertyName) == c_checkedString)
{
// if checked
// uncheck
series.SetCustomProperty(c_checkCustomPropertyName, c_uncheckedString);
series.CustomProperties = series.CustomProperties; // workaround - trigger change - is this a bug?
// hide in mainChart
mainChart.Series[seriesNo].Enabled = false;
}
else
{
// if unchecked
legendChart.Series[seriesNo].SetCustomProperty(c_checkCustomPropertyName, c_checkedString);
series.CustomProperties = series.CustomProperties; // workaround - trigger change - is this a bug?
// show in mainChart
mainChart.Series[seriesNo].Enabled = true;
}
}
}
private void contextMenu_Opening(object sender, CancelEventArgs e)
{
Point mousePosition = legendChart.PointToClient(Control.MousePosition);
int seriesNo = mousePosition.Y / c_legendItemHeight;
Series series = legendChart.Series[seriesNo]; // TODO - check if not out of range
contextMenu.Items.Clear();
string state = series.GetCustomProperty(c_checkCustomPropertyName) == c_checkedString ? "visible" : "hidden";
contextMenu.Items.Add("&Some strange action for " + state + " item named " + series.Name);
contextMenu.Items.Add("&Another action ...");
}
}
结果应如下所示: