Exemple #1
0
        private static ScatterSeries ProvideScatterSeries(PropertyInfo propertyInfo, string dependentValuePath,
                                                          string independentValuePath)
        {
            var series = new ScatterSeries();

            series.SetBinding(DataPointSeries.ItemsSourceProperty, propertyInfo.GetBinding());
            series.IndependentValuePath = independentValuePath;
            series.DependentValuePath   = dependentValuePath;

            series.Unloaded += (o, e) =>
            {
                var control = o as ScatterSeries;
                if (control == null)
                {
                    return;
                }

                BindingOperations.ClearAllBindings(control);
            };

            return(series);
        }
Exemple #2
0
        // note: we have modified this, but probably very poorly! WWJC do?
        private static void SeriesSourceChanged(DependencyObject d,
                                                DependencyPropertyChangedEventArgs e)
        {
            if (!(d is Chart))
            {
                throw new Exception("Series attached property only works on a Chart type");
            }

            var chart = d as Chart;

            /* Get our collection of data we need for each series */
            var chartSeriesSource = e.NewValue as PlotPointCollection;

            //if (chartSeriesSource.Count == 0) // JWC was here.
            chart.Series.Clear();

            if (chartSeriesSource == null)
            {
                throw new Exception("The SeriesSource does not support IEnumerable");
            }

            // TODO: added this in for last minute labeling - needs revision! DJC
            if (chart.DataContext is PlotViewModel)
            {
                var vm = chart.DataContext as PlotViewModel;
                chart.Title = vm.Title;

                // attempt to reduce the font size:
                var titleStyle = new Style(typeof(Title))
                {
                    BasedOn = GetChartTitleStyle(chart)
                };
                var s  = new Setter(TextBlock.FontSizeProperty, 18);                      // todo: don't think these are working
                var s2 = new Setter(TextBlock.FontWeightProperty, FontWeights.ExtraBold); // todo: don't think these are working
                titleStyle.Setters.Add(s);
                titleStyle.Setters.Add(s2);
                chart.TitleStyle = titleStyle;
            }

            /* Loop over each collection of data */
            int dataSourceCounter = 0;
            int colorLabelCounter = 0;

            for (int i = 0; i < chartSeriesSource.Count; i++)
            {
                var dataSource = chartSeriesSource[i];

                //DynamicSeries series; CKH: dynamicseries replaced by datapointseries
                DataPointSeries series;

                /* Find out what type of series we want to use */
                var seriesType = GetSeriesType(chart);

                switch (seriesType)
                {
                case SeriesType.Line:
                    series = new LineSeries();
                    break;

                case SeriesType.Bar:
                    series = new BarSeries();
                    break;

                case SeriesType.Column:
                    series = new ColumnSeries();
                    break;

                case SeriesType.Pie:
                    series = new PieSeries();
                    break;

                case SeriesType.Scatter:
                    series = new ScatterSeries();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                /* Get and set the style of the newly created series */
                var seriesStyle = GetSeriesStyle(chart);
                series.Style = seriesStyle;

                string titleBindingName = null;

                // TODO: added this in for last minute labeling - needs revision! DJC
                if (chart.DataContext is PlotViewModel)
                {
                    var vm = chart.DataContext as PlotViewModel;
                    series.Title = vm.Labels[dataSourceCounter];

                    //if(true)
                    var s =
                        new Setter(
                            Control.BackgroundProperty,
                            new SolidColorBrush(
                                DefaultColorList[colorLabelCounter % DefaultColorList.Count]));

                    if (i >= chartSeriesSource.Count - 1 || chartSeriesSource.ColorTags[i + 1] != "repeat") // advance the color, unless the group name is "repeat"
                    {
                        colorLabelCounter += 1;
                    }

                    switch (seriesType)
                    {
                    case SeriesType.Line:
                    case SeriesType.Bar:
                    case SeriesType.Column:
                    case SeriesType.Scatter:
                    default:
                        var dpSeries = ((DataPointSingleSeriesWithAxes)series);
                        dpSeries.DataPointStyle = new Style(typeof(LineDataPoint))
                        {
                            BasedOn = GetDataPointSeriesStyle(chart)
                        };
                        dpSeries.DataPointStyle.Setters.Add(s);
                        break;

                    case SeriesType.Pie:
                        ((PieSeries)series).Style.Setters.Add(s);
                        break;
                    }
                }
                else
                {
                    titleBindingName = GetTitle(chart); // this is getting the title from the attached property, which we could easily bind to...
                }

                if (!string.IsNullOrEmpty(titleBindingName))
                {
                    /* Do some binding of the Title property */
                    var titleBinding = new Binding(titleBindingName)
                    {
                        Source = series.Title,
                        Mode   = BindingMode.TwoWay
                    };

                    series.SetBinding(Series.TitleProperty, titleBinding);
                }

                /* Setup the bindings configured in the attached properties */
                series.DependentValueBinding   = new Binding(GetDependentValueBinding(chart));
                series.IndependentValueBinding = new Binding(GetIndependentValueBinding(chart));

                /*Set the ItemsSource property, which gives the data to the series to be rendered */
                series.ItemsSource = dataSource as IEnumerable;

                /* Add the series to the chart */
                chart.Series.Add(series);

                dataSourceCounter++;
            }
        }