0

How do I show string values on the x axis of a chart control ? the datapoints (x & y) are both double. I have an IEnumerable containing a list of objects with 2 properties. The name of a report and an integer value indicating how many times it was run. so the integer is on the y axis and the report name is on the x axis. But I only seem to have double options for the value types. So when I create my datapoint, Im geting an error

DataPoint p1 = new DataPoint();
p1.XValue = log.ReportName; ---> this is invalid
p1.YValues = new Double[] { log.ReportTotal };

how can this be done ?

Ive tried this

reportTotal.XValueType = ChartValueType.String;

and when I plot my datapoints, Ive done this

        int i = 1;
        foreach (var log in this._reportLogs)
        {
            DataPoint p1 = new DataPoint();
            p1.XValue = (double)i;
            p1.YValues = new Double[] { log.ReportFrequency };
            i++;
            reportTotal.Points.Add(p1);

        }

but now all im getting along the x axis are the number of the i varaible, how on earth do I just get the content of the text property ?

this is how I made it work

Dictionary<string, int> ReportLogs = new Dictionary<string, int>();

foreach (var log in this._reportLogs)
      ReportLogs.Add(log.ReportName, log.ReportFrequency);

foreach (KeyValuePair<string, int> log in ReportLogs)
       reportTotal.Points.AddXY(log.Key, log.Value);
proteus
  • 545
  • 2
  • 12
  • 36

1 Answers1

0

The labels along the axis are created from the x-values and their number is determined either automatically as they fit or by setting the Interval property of the x-axis.

So their placement will not necessarily coincide with the actual DataPoints.

And you can format them but only within the normal formatting rules, ie you can't get creative with expressions to convert a number to a custom string, which seems to be just what you want: show a report name according to its number.

Other examples could be show a city from its zip code or a person name acording to emp.ID..

To gain full control you may need to use CustomLabels. They are a little bit tricky but quite useful. See several of these posts for numerous examples!

Also note: If you change the x-value type to string (which is possible : yourseries.XValueType = ChartValueType.String) these strings would show directly but you would loose the actual x-values! Usually not recommended (but possibly fine in your case)!

So if you want to can do it; however you can't prepare the DataPoint like you did. Instead you need to use the Points.AddXY method.

This will convert the strings to double (resulting in zeroes) and copy the string into the labels..:

  reportTotal.Points.AddXY(log.ReportName, new Double[] { log.ReportTotal });

Update

Another way would be to set the AxisLabel of each DataPoint. For all of these to show up you may need to set the yourxaxis.Interval = 1.

So in your loop simply add

p1.AxisLabel = log.ReportName;
TaW
  • 53,122
  • 8
  • 69
  • 111
  • I did actually set XValueType as a string, but how do I set the x datapoint value from a string ? its always a double – proteus Mar 02 '18 at 10:33
  • just updated my question, still cant get this to work, surely something so simple cant be this difficult ? – proteus Mar 02 '18 at 10:52
  • Indeed, the values are always doubles and when you add them as strings then the values will be zero. To add them as strings you can use the Points.AddXY method. See my update! – TaW Mar 02 '18 at 11:14
  • See my last update, which is probably what you should do! – TaW Mar 02 '18 at 11:33