Skip to main content

Microsoft Silverlight

Answered Question Changing Column Chart Bar Width Through CodeRSS Feed

(0)

tsmccartan
tsmccartan

Member

Member

8 points

19 Posts

Changing Column Chart Bar Width Through Code

Can you create and set a style programatically through code?  Like the following:

 

Style ChartSeriesStyle = new Style();

ChartSeriesStyle.TargetType = typeof(ColumnDataPoint);

ChartSeriesStyle.SetValue(ColumnDataPoint.MinWidthProperty,2);

ChartSeriesStyle.SetValue(ColumnDataPoint.MaxWidthProperty, 2);

((ColumnSeries)this.MainChart.Series[0]).DataPointStyle = ChartSeriesStyle;

((ColumnSeries)this.MainChart.Series[0]).ItemsSource = chartData;

 

This crashes on me, so I don't think it is correct.  Any help would be appreciated.

Thanks

lee_sl
lee_sl

Contributor

Contributor

2992 points

585 Posts

Re: Changing Column Chart Bar Width Through Code

try

ChartSeriesStyle.SetValue(ColumnDataPoint.MinWidthProperty,2.0);

ChartSeriesStyle.SetValue(ColumnDataPoint.MaxWidthProperty, 2.0);

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

tsmccartan
tsmccartan

Member

Member

8 points

19 Posts

Re: Changing Column Chart Bar Width Through Code

Thanks for the reply.  Unfortunatly, this still causes the same error:

Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

I have another related question. Is there a way to change the individual bars in a chart to a different color, through code? I have to highlight a specific bar, based on its values after binding.

Thanks again,

Tom

JustinAngel
JustinAngel

Contributor

Contributor

4415 points

596 Posts

Answered Question

Re: Changing Column Chart Bar Width Through Code

Here's the XAML code needed to set the style:

        <charting:Chart x:Name="chart">

            <charting:ColumnSeries ItemsSource="{Binding}"

                                  DependentValueBinding="{Binding Value}"

                                  IndependentValueBinding="{Binding Key}">

                <charting:ColumnSeries.DataPointStyle>

                    <Style TargetType="charting:ColumnDataPoint">

                        <Setter Property="MaxWidth" Value="20" />

                    </Style>

                </charting:ColumnSeries.DataPointStyle>

            </charting:ColumnSeries>

        </charting:Chart>

 

    chart.DataContext = new KeyValuePair<string, int>[]

            {

                new KeyValuePair<string, int>("hello", 1),

                new KeyValuePair<string, int>("world", 2),

                new KeyValuePair<string, int>("foo", 3),

                new KeyValuePair<string, int>("bar", 4)

            };

If you want to specify the DataPointStyle in code you'll have to use the StylePalette on the chart.

        <charting:Chart x:Name="chart">

            <charting:ColumnSeries ItemsSource="{Binding}"

                                  DependentValueBinding="{Binding Value}"

                                  IndependentValueBinding="{Binding Key}">

            </charting:ColumnSeries>

        </charting:Chart>

 

        public SilverlightControl32()

        {

            InitializeComponent();

 

            System.Windows.Style DataPointStyle = new Style(typeof(ColumnDataPoint));

            DataPointStyle.Setters.Add(new Setter(ColumnDataPoint.MaxWidthProperty, 10.0));

            StylePalette palette = new StylePalette() { DataPointStyle };

            chart.StylePalette = palette;

 

            this.Loaded += new RoutedEventHandler(SilverlightControl32_Loaded);

        }

 

        void SilverlightControl32_Loaded(object sender, RoutedEventArgs e)

        {

            chart.DataContext = new KeyValuePair<string, int>[]

            {

                new KeyValuePair<string, int>("hello", 1),

                new KeyValuePair<string, int>("world", 2),

                new KeyValuePair<string, int>("foo", 3),

                new KeyValuePair<string, int>("bar", 4)

            };

        }

There seems to be a bug setting the DataPointStyle once the series has been initialized from XAML, so working through the StylePalette is your best bet. 
I've opened up a Silverlight Toolkit codeplex issue to document that DataPointStyle itself is not respected when set from code:
http://www.codeplex.com/Silverlight/WorkItem/View.aspx?WorkItemId=1302  
If you'd like to see this issue fixed, please feel free to vote on it.

 

--
Justin Angel,
Blog @ http://silverlight.net/blogs/JustinAngel
Twitter @ http://twitter.com/JustinAngel
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities