2013-03-08 123 views
1

我需要将图表导出为图像,而不是先在WPF中显示它。所以我建立了图表代码:visifire自定义轴标签不显示

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); 
    } 

一切工作正常,除了自定义轴标签丢失。这是输出的样子:

Chart Output

正如你所看到的,甚至还有分配给CustomAxis标签空间,但它们没有显示。任何人有想法?

提示:AnimationEnabled已被设置为false,否则,拍照时的一系列尚未呈现 - 我花了很长时间才明白这一点。

+0

您能否告诉我CustomAxisLabel的“From”和“To”属性中设置了哪些值? – 2013-03-11 10:18:39

回答

1

我已经找到了解决方案:

当超过界限,将值设置为Double.NaN。我发现,所有标签的创建都会失败,如果集合Double.Nan或Double.Infinity中有任何值 - 似乎是visifire中的一个bug。

我解决了它通过在其单独的集合中添加每个标签。