Search code examples
c#visifire

visifire Custom Axis Labels Not Showing


I need to Export my Chart as an image without showing it first in WPF. So i built the Chart in Code:

    public void CreateHistogram(CalcRepository cr, int i)
    {
        Chart chart = new Chart();

        chart.Width = 300;
        chart.Height = 200;
        chart.ScrollingEnabled = false;
        chart.AnimationEnabled = false;
        chart.TrendLines.Add(new TrendLine{Value = cr.Mean,Orientation = System.Windows.Controls.Orientation.Vertical});
        chart.TrendLines.Add(new TrendLine{Value = cr.ChartTrippleNegativeStdDeviation,Orientation = System.Windows.Controls.Orientation.Vertical,LineStyle = LineStyles.Dashed});chart.TrendLines.Add(new TrendLine{Value = cr.ChartTripplePositiveStdDeviation,Orientation = System.Windows.Controls.Orientation.Vertical,LineStyle = LineStyles.Dashed});
        chart.TrendLines.Add(new TrendLine{Value = cr.UpperSpecificationLimit,Orientation = System.Windows.Controls.Orientation.Vertical});
        chart.TrendLines.Add(new TrendLine{Value = cr.LowerSpecificationLimit,Orientation = System.Windows.Controls.Orientation.Vertical});
        chart.TrendLines[0].SetValue(Canvas.ZIndexProperty, 40);
        chart.TrendLines[1].SetValue(Canvas.ZIndexProperty, 40);
        chart.TrendLines[2].SetValue(Canvas.ZIndexProperty, 40);
        chart.TrendLines[3].SetValue(Canvas.ZIndexProperty, 40);
        chart.TrendLines[4].SetValue(Canvas.ZIndexProperty, 40);



        chart.DataPointWidth = cr.DataPointWidth;
        chart.Visibility = Visibility.Visible;
        Axis x = new Axis();
        x.AxisMaximum = cr.VisUpperBound;
        x.AxisMinimum = cr.VisLowerBound;
        x.AxisType = AxisTypes.Primary;

        CustomAxisLabels cal = new CustomAxisLabels();
        cal.Labels.Add(new CustomAxisLabel {From = cr.Mean, To = cr.Mean, Text = "Mean"});
        cal.Labels.Add(new CustomAxisLabel {From = cr.ChartTrippleNegativeStdDeviation,To = cr.ChartTrippleNegativeStdDeviation,Text = "LCL"});
        cal.Labels.Add(new CustomAxisLabel{From = cr.ChartTripplePositiveStdDeviation,To = cr.ChartTripplePositiveStdDeviation,Text= "UCL"});
        cal.Labels.Add(new CustomAxisLabel {From = cr.UpperSpecificationLimit, To = cr.UpperSpecificationLimit , Text = "USL"});
        cal.Labels.Add(new CustomAxisLabel {From = cr.LowerSpecificationLimit, To = cr.LowerSpecificationLimit, Text = "LSL"});

        cal.FontSize = 10;
        cal.Angle = 0;
        cal.FontColor = new SolidColorBrush(Colors.Black);
        cal.Enabled = true;


        x.CustomAxisLabels.Add(cal);
        chart.AxesX.Add(x);

        var ds = new DataSeries();
        var dpc = new DataPointCollection(cr.HistogramValues);
        ds.DataPoints = dpc;

        chart.Series.Add(ds);


        ds.ZIndex = 1;
        ds.Bevel = false;
        ds.ShadowEnabled = false;
        ds.LightingEnabled = false;
        ds.Color = new SolidColorBrush(Colors.SteelBlue);


        chart.BeginInit();
        chart.EndInit();
        chart.Measure(new Size(300, 200));
        chart.Arrange(new Rect(0, 0, 300, 200));
        chart.UpdateLayout();

        ExportToPng(new Uri("C:\\" + i + ".png"), chart);
    }

everything works fine, except the custom Axis Labels are missing. This is how the Output looks like:

Chart Output

As you can see, there is even space allocated for the CustomAxis Labels but they are not shown. Anyone got an idea?

Hint: AnimationEnabled has to be set false otherwise the series is not rendered yet when the image is taken - took me a long time to figure that out.


Solution

  • I found already a solution:

    When exceeding the bounds, the values are set to Double.NaN. I found out, that the creation of all Labels fails, if any value in the Collections Double.Nan or Double.Infinity - Seems lika a bug in visifire.

    I solved it by adding each Label in its seperate Collection.