Search code examples
c#mysqlchartsxvalue

C# Chart control : X value of a horizontal bar chart is 0 for all points in the serie


I have a horizontally stacked bar chart in my app, and I'm trying to figure out how to get the X axis value when the user clicks on a bar. The problem is when I look at the values at runtime, the Y values are fine but the X values are all 0.

Screen capture

In the image above, light blue bars are from Series[0] and represent the MTD sales and the darker ones from Series[1] represent last year's sales for the same month. My goal is that when the user double-clicks on a bar, he is taken to the detailed sales report for that salesperson.

I haven't tried switching my chart to a regular bar chart because this is what I will need to look like in the end. But I'm starting to wonder if the reason I'm getting all 0's in the XValue field is because of this or because I am using strings as the type of value.

Has anyone ever encountered this or have any clues as to how to fix it?

Screen capture of points at runtime


Solution

  • OK so I finally managed to put custom labels on the chart using the Chart.Customize event.

    Here is the data I am using for this chart:

    Vendeur     |   Total    |    idDepartement   |   idEmploye   | TotalLastYear
    Ghislain        5256.30       1                   56                0.00
    Kim            12568.42       1                    1            74719.18
    Philippe       17565.27       1                   44            38454.85
    

    I changed X axis XValueType to Double, and the XValueMember to idEmploye.

    Result

    As you can see, it's not very user friendly to have employee id's on a chart. This is where the Customize event gets useful.

    Here is my event:

    private void chart1_Customize(object sender, EventArgs e)
        {
            // Set X axis interval to 1, label will be centered (between 0.5 and 1.5)
            chart1.ChartAreas[0].AxisX.Interval = 1;
            double startOffset = 0.5;
            double endOffset = 1.5;
    
            chart1.ChartAreas[0].AxisX.CustomLabels.Clear();
    
            // Cycle through chart Datapoints in first serie
            foreach (System.Windows.Forms.DataVisualization.Charting.DataPoint pt in chart1.Series[0].Points)
            {
    
                // First get the dataset used for the chart (from its bindingsource)
                DataSet dsSales = (DataSet)bsViewVentesParUtilisateurSignsMoisCourant.DataSource;
    
                // Second get the datatable from that dataset based on the datamember 
                // property of the bindingsource
                DataTable dtSales = (DataTable)dsSales.Tables[bsViewVentesParUtilisateurSignsMoisCourant.DataMember];
    
                // Get a dataview and filter its result based on the current point's XValue
                DataView dv = new DataView(dtSales);
                dv.RowFilter = "idEmploye=" + pt.XValue.ToString();
    
                // Get the "Salesperson" (or "Vendeur") column value from the first result
                // (other rows will have the same value but row 0 is safe)
                string firstname = dv.ToTable().Rows[0].Field<string>("Vendeur").ToString();
    
                // Create new customlabel and add it to the X axis
                CustomLabel salespersonLabel = new CustomLabel(startOffset, endOffset, firstname, 0, LabelMarkStyle.None);
                chart1.ChartAreas[0].AxisX.CustomLabels.Add(salespersonLabel);
                startOffset += 1;
                endOffset += 1;
            }
        }
    

    Final result

    I am very happy with the result. And now when I double-click on a bar in the chart, I can get the employee id from the X value and generate the code to get the detailed sales report for that person for the given month.