首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# .NET图表-添加图例滚动条和复选框

C# .NET图表-添加图例滚动条和复选框
EN

Stack Overflow用户
提问于 2014-07-30 19:44:06
回答 1查看 4.7K关注 0票数 2

我正在使用Visual 2013中的图表类来可视化我的一些数据。然而,我的数据很快产生了许多系列,这是非常重要的,让他们都在一个图表。我限制了图例面积的20%完整的图表面积,所以我几乎不能显示超过7-8图例项目时,我拉伸我的图表到它的最大大小。控制装置只会..。在图例项目的空间耗尽之后。

它不只是写.,它是否有可能添加一个滚动条到图例,并能够看到所有的项目?我知道我可以在某种程度上实现我自己的传奇,但我想从图表类提供的东西中榨取最多。我还想在每个图例项目旁边添加复选框,这将指示是否应该隐藏在图表上。如果没有我自己的图例实现,这能行吗?

此外,我也希望有一个菜单展开右击一个图例项目的几个选项,但这是完全可选的。滚动条和复选框现在是我的主要问题。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-30 22:15:27

一般想法:你必须创建两个图表。一个是主要的,第二个是传奇。如果序列顺序相同,您将拥有相同的系列样式。

用于显示右击图例项目上的弹出:

将ContextMenu (工具箱中的ContextMenuStrip类)连接到图例的图表。

为了向传奇人物展示隐藏系列:

您必须实现MouseClick事件处理程序,并使用数学检查鼠标光标下的对象(GetChildAtPoint()方法不适用于图例项)。等式:是series_index = control_relative_mouse_y / c_legendItemHeight,其中c_legendItemHeight是计算控件高度(单个图例项的高度)所提供的值。您必须将图例图配置为包含LegendStyleRowMaximumAutoSize100DockingLeftIsTextAutoFitfalseIsEquallySpacedItemstrue。您在图例中定义了3列(一列用于系列样式,第二列用于复选框,第三列用于系列名称)。使用系列CustomProperties保持可见性状态。在check列中,使用此自定义属性(Text = "#CUSTOMPROPERTY(...)")显示check状态。图表不支持自动调整大小。你可以手动完成。在系列加载期间,将图表高度设置为计算值。此值等于_stock.Shares.Count * c_legendItemHeight + 9。其中:_stock.Shares.Count是图例中的项目数,c_legendItemHeight常量的项目高度(整数值,数字栅格,然后18似乎适合我),9 (似乎是常量)。我知道这不太好,但我找不到更好的解决办法。我在我的例子中添加了502系列,它运行得很好。请确保您的图表中没有任何空白,因为否则您将无法正确计算序列号。

关于“传奇中的许多系列”的问题:

将图例图放入打开AutoScroll属性的面板中。使用上述描述的表达式设置面板和图例高度。

源代码:

代码语言:javascript
复制
    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 ...");
        }
    }

结果应该如下所示:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25045594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档