private void DataBindChart(UI.DataVisualization.Charting.Chart chart) { // NOTE: WebForms chart will throw null refs if optional values are set to null if (_dataSource != null) { if (!String.IsNullOrEmpty(_dataSource.GroupByField)) { chart.DataBindCrossTable( _dataSource.DataSource, _dataSource.GroupByField, _dataSource.XField ?? String.Empty, _dataSource.YFields, _dataSource.OtherFields ?? String.Empty, _dataSource.PointSortOrder); } else if (_dataSource.DataBindTable) { chart.DataBindTable( _dataSource.DataSource, _dataSource.XField ?? String.Empty); } else { Debug.Assert(false, "Chart.DataBind was removed - should not reach here"); //chart.DataSource = _dataSource.DataSource; //chart.DataBind(); } } }
private static void LoadChartThemeFromFile( UI.DataVisualization.Charting.Chart chart, Stream templateStream ) { // workarounds for Chart templating bugs mentioned in: // http://social.msdn.microsoft.com/Forums/en-US/MSWinWebChart/thread/b50d5b7e-30e2-4948-af7a-370d9be1268a chart.Serializer.Content = SerializationContents.All; chart.Serializer.SerializableContent = String.Empty; // deserialize all content chart.Serializer.IsTemplateMode = true; chart.Serializer.IsResetWhenLoading = false; // loading serializer with stream to avoid bug with template file getting locked in VS // The default xml reader used by the serializer does not ignore comments // Using the IsUnknownAttributeIgnored fixes this, but then it would give no feedback to the user // if member names do not match the spelling and casing of Chart properties. XmlReader reader = XmlReader.Create( templateStream, new XmlReaderSettings { IgnoreComments = true } ); chart.Serializer.Load(reader); }
private void ApplySeries(UI.DataVisualization.Charting.Chart chart) { foreach (var seriesData in _series) { var series = new Series(); try { series.AxisLabel = seriesData.AxisLabel ?? String.Empty; series.ChartArea = seriesData.ChartArea ?? String.Empty; series.ChartType = seriesData.ChartType; series.Legend = seriesData.Legend ?? String.Empty; series.MarkerStep = seriesData.MarkerStep; series.Name = seriesData.Name ?? String.Empty; // data-bind the series (todo - support o.Points.DataBind()) if (seriesData.DataSource != null) { if (String.IsNullOrEmpty(seriesData.DataSource.YFields)) { var yValues = seriesData.DataSource.DataSource; var yValuesArray = yValues as IEnumerable[]; if ((yValuesArray != null) && !(yValues is string[])) { series.Points.DataBindXY( seriesData.DataSource.XDataSource, yValuesArray ); } else { series.Points.DataBindXY( seriesData.DataSource.XDataSource, yValues ); } } else { series.Points.DataBindXY( seriesData.DataSource.XDataSource, seriesData.DataSource.XField, seriesData.DataSource.DataSource, seriesData.DataSource.YFields ); } } } catch (Exception) { // see notes above series.Dispose(); throw; } chart.Series.Add(series); } }
// Notes on ApplyXXX methods: // Chart elements should be configured before they are added to the chart, otherwise there // will be some rendering problems. // We must catch all exceptions when configuring chart elements and dispose of them manually // if they have not been added to the chart yet, otherwise FxCop will complain. private void ApplyChartArea(UI.DataVisualization.Charting.Chart chart) { ChartArea chartArea = new ChartArea("Default"); try { ApplyAxis(chartArea.AxisX, _xAxis); ApplyAxis(chartArea.AxisY, _yAxis); chart.ChartAreas.Add(chartArea); } catch { // This is to appease FxCop chartArea.Dispose(); throw; } }
private void ApplyTitles(UI.DataVisualization.Charting.Chart chart) { foreach (var titleData in _titles) { var title = new Title(); try { title.Name = titleData.Name; title.Text = titleData.Text; } catch (Exception) { // see notes above title.Dispose(); throw; } chart.Titles.Add(title); } }
private void ApplyLegends(UI.DataVisualization.Charting.Chart chart) { foreach (var legendData in _legends) { var legend = new Legend(); try { legend.Name = legendData.Name ?? String.Empty; legend.Title = legendData.Title ?? String.Empty; } catch (Exception) { // see notes above legend.Dispose(); throw; } chart.Legends.Add(legend); } }
// create and execute an action against the WebForm control in a limited scope since the control is disposable. internal void ExecuteChartAction(Action <UI.DataVisualization.Charting.Chart> action) { using (UI.DataVisualization.Charting.Chart chart = new UI.DataVisualization.Charting.Chart()) { chart.Width = new Unit(_width); chart.Height = new Unit(_height); ApplyChartArea(chart); ApplyLegends(chart); ApplySeries(chart); ApplyTitles(chart); DataBindChart(chart); // load the template last so that it can be applied to all the chart elements LoadThemes(chart); action(chart); } }
private void LoadThemes(UI.DataVisualization.Charting.Chart chart) { if (!String.IsNullOrEmpty(_theme)) { using (MemoryStream memoryStream = new MemoryStream()) { byte[] themeContent = Encoding.UTF8.GetBytes(_theme); memoryStream.Write(themeContent, 0, themeContent.Length); memoryStream.Seek(0, SeekOrigin.Begin); LoadChartThemeFromFile(chart, memoryStream); } } if (!String.IsNullOrEmpty(_themePath)) { using (Stream stream = _virtualPathProvider.GetFile(_themePath).Open()) { LoadChartThemeFromFile(chart, stream); } } }