0

I am building a winform point chart using C# where the data comes from a datatable. I've created the points like this:

chart1.Series.Add("series1");
chart1.Series["series1"].ChartType = SeriesChartType.Point;
chart1.Series["series1"].YValueMembers = "VALUE";
chart1.Series["series1"].XValueMember = "DATE";
chart1.DataSource = dt;

I'd like to be able make a legend of the points on the chart using different colors/symbols based on a third column in the datatable dt called product. I've tried a number of things to get this done but nothing is working. How can I accomplish this?

ejyoung
  • 81
  • 1
  • 8
  • https://msdn.microsoft.com/en-us/library/dd456711.aspx – Chetan Aug 22 '18 at 04:09
  • Is this a Pie chart? Other types will by default show one legendItem per series. If you want something different, like legenditems for each point you wil need to create a new legend along with all the items in code. [Here is an example](https://stackoverflow.com/questions/35791944/how-to-add-data-table-with-legend-keys-to-a-ms-chart-in-c/35795254#35795254) of creating a custom legend. - You will not be able to use databinding on a legend btw. - Please elaborate more on what you want..! – TaW Aug 22 '18 at 06:43
  • I realized my problem this morning - I'll update/answer my own question for future reference. Thanks! – ejyoung Aug 22 '18 at 19:23

2 Answers2

5

You can use this code I got from msdn page

// Create a new legend called "Legend2".
chart1.Legends.Add(new Legend("Legend2"));

// Set Docking of the Legend chart to the Default Chart Area.
chart1.Legends["Legend2"].DockToChartArea = "Default"; 

// Assign the legend to Series1.
chart1.Series["Series1"].Legend = "Legend2";
chart1.Series["Series1"].IsVisibleInLegend = true;
Pietro Nadalini
  • 1,722
  • 3
  • 13
  • 32
0

Part of my problem was first understanding how to construct charts in the first place. I realized I needed to create a series for each unique product in my datatable and add those to the chart. I first made a list of unique products in the datatable:

        List<string> products = new List<string>();
        foreach (DataRow row in dt.Rows)
        {
            if (!products.Contains(row["product"].ToString()))
            {
                products.Add(row["product"].ToString());
            }
        }

Then

       foreach (string product in products)
        {
            string seriesName = product;
            chart1.Series.Add(seriesName);
            DataTable dtprod = new DataTable();
            dtprod = dt.Select("product= '" + product + "'").CopyToDataTable();
            chart1.Series[seriesName].ChartType = SeriesChartType.Point;
            chart1.Series[seriesName].YValueMembers = "VALUE";
            chart1.Series[seriesName].XValueMember = "DATE";
            chart1.Series[seriesName].Points.DataBindXY(dtprod.Rows,"DATE", dtprod.Rows, "VALUE");
            chart1.Series[seriesName].XValueType = ChartValueType.DateTime;
            chart1.Series[seriesName].MarkerSize = 10;
        }

This produced a point chart with a unique marker for each product. Thanks for your answers!

ejyoung
  • 81
  • 1
  • 8