Quantcast
Channel: OxyPlot (moved to GitHub)
Viewing all articles
Browse latest Browse all 2061

New Post: Checkable legend

$
0
0
I'll add an example of how I'm handling it for WinForms! Although keep in mind this is for LineSeries and LinearAxis only, it could probably easily be extended to handle more. Also I've been thinking of writing it to detect and add only newly added series and axis, but haven't had the time at work yet. An example of the result can be found on this post
///<summary>///     Refreshes the custom legend based on the axes and series in the PlotModel.///</summary>privatevoid UpdateCustomLegend()
{
    uiSeriesLegendLayout.Controls.Clear();
    uiAxesLegendLayout.Controls.Clear();

    foreach (LineSeries s in PlotModel.Series.OfType<LineSeries>().OrderBy(s => s.Title))
    {
        var seriesColorButton = new Button
        {
            AutoSize = true,
            BackColor = s.Color.ToColor(),
            FlatStyle = FlatStyle.Flat,
            Size = new Size(30, 15),
            Tag = s.Tag,
            UseVisualStyleBackColor = false
        };
        seriesColorButton.Click += (sender, args) =>
        {
            using (var d = new ColorDialog())
            {
                d.Color = s.Color.ToColor();
                if (d.ShowDialog() != DialogResult.OK)
                    return;

                s.Color = d.Color.ToOxyColor();
                ((Button) sender).BackColor = d.Color;
                PlotModel.InvalidatePlot(false);
            }
        };

        var seriesVisibilityCheckBox = new CheckBox
        {
            AutoSize = true,
            Checked = s.IsVisible,
            Tag = s.Tag,
            Text = s.Title
        };
        seriesVisibilityCheckBox.CheckedChanged += (sender, args) =>
        {
            s.IsVisible = ((CheckBox) sender).Checked;
            PlotModel.InvalidatePlot(false);
        };

        uiSeriesLegendLayout.Controls.Add(seriesColorButton);
        uiSeriesLegendLayout.Controls.Add(seriesVisibilityCheckBox);
        uiSeriesLegendLayout.SetFlowBreak(seriesVisibilityCheckBox, true);
    }

    var seriesSelectAllCheckBox = new CheckBox
    {
        AutoSize = true,
        Checked = true,
        Text = "Select All"
    };
    seriesSelectAllCheckBox.CheckedChanged += (sender, args) =>
    {
        foreach (CheckBox cb in uiSeriesLegendLayout.Controls.OfType<CheckBox>())
            cb.Checked = ((CheckBox) sender).Checked;
    };

    uiSeriesLegendLayout.Controls.Add(seriesSelectAllCheckBox);

    var hideLinkedSeriesCheckBox = new CheckBox
    {
        AutoSize = true,
        Checked = false,
        Text = "Hide Linked Series"
    };

    foreach (LinearAxis a in PlotModel.Axes.OfType<LinearAxis>())
    {
        var axisVisibilityCheckBox = new CheckBox
        {
            AutoSize = true,
            Checked = a.IsAxisVisible,
            Tag = a.Tag,
            Text = a.Title
        };
        axisVisibilityCheckBox.CheckedChanged += (sender, args) =>
        {
            a.IsAxisVisible = ((CheckBox) sender).Checked;
            PlotModel.InvalidatePlot(false);

            if (!hideLinkedSeriesCheckBox.Checked)
                return;

            foreach (
                LineSeries s in
                    PlotModel.Series.OfType<LineSeries>()
                             .Where(s => s.YAxisKey == a.Key || s.XAxisKey == a.Key))
            {
                s.IsVisible = a.IsAxisVisible;
                uiSeriesLegendLayout.Controls.OfType<CheckBox>()
                                    .Single(cb => cb.Tag == s.Tag)
                                    .Checked = s.IsVisible;
            }
        };

        uiAxesLegendLayout.Controls.Add(axisVisibilityCheckBox);
        uiAxesLegendLayout.SetFlowBreak(axisVisibilityCheckBox, true);
    }

    var axisSelectAllCheckBox = new CheckBox
    {
        AutoSize = true,
        Checked = true,
        Text = "Select All"
    };
    axisSelectAllCheckBox.CheckedChanged += (sender, args) =>
    {
        foreach (
            CheckBox cb in
                uiAxesLegendLayout.Controls.OfType<CheckBox>()
                                  .Where(cb => cb != hideLinkedSeriesCheckBox))
            cb.Checked = ((CheckBox) sender).Checked;
    };

    uiAxesLegendLayout.Controls.Add(axisSelectAllCheckBox);
    uiAxesLegendLayout.Controls.Add(hideLinkedSeriesCheckBox);
}

Viewing all articles
Browse latest Browse all 2061

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>