Changing LineGraph Stroke or StrokeThickness doesn't update the legend - solved

May 27, 2009 at 8:29 PM
Edited May 27, 2009 at 8:32 PM

I have a requirement for a line graph to highlight when the mouse is over it. This is done by simply changing StrokeThickness on MouseEnter/MouseLeave. However, the line shown in the legend doesn't update :-(

The legend doesn't update because both LineGraph.Stroke & StrokeThickness will generate a new pen if the existing pen is sealed (which is the correct thing to do). The result is that the legend and the line graph end up using 2 different pen instances.

Here's how I've fixed this:

  1. Made ViewportElement2D.RaisePropertyChanged protected
  2. Added 'RaisePropertyChanged("LinePen");' to LineGraph.LinePen setter
  3. Added a OnChartPropertyChanged method to PenDescription (code below)
  4. Subscribed to LineGraph.PropertyChanged event in PenDescription.AttachCore
  5. Added protected virtual void DetachCore(UIElement element) { } to Description
  6. Overrode DetachCode in PenDescription to unsubscribe from LineGraph.PropertyChanged.

So basically the PenDescription class listens for changes to LineGraph.LinePen and updates as appropriate. It'd be great if these changes could be incorporated into the next release.

Here's the additional PenDescription methods:

 

protected override void DetachCore(UIElement graph)

{

base.DetachCore(graph);

LineGraph g = graph as LineGraph;

if (g != null)

{

g.PropertyChanged -= OnChartPropertyChanged;

}

}

 private void OnChartPropertyChanged(object sender, PropertyChangedEventArgs e)

{

if (e.PropertyName == "LinePen")

{

LineGraph g = sender

as LineGraph;

if (g != null)

{

 pen = g.LinePen;

// This resets the content, causing the bindings to refire & pick up the new pen.

Description d = LegendItem.Description;

LegendItem.Description = null;

 LegendItem.Description = d;

}

}

}

Editor
Jun 26, 2009 at 5:26 PM

Hi,

 

Thanks for your solution, I'm glad that you have reached what you wanted.

I think we'll change legend system a little in future - probably, we'll use binding to bind legend items' properties to their sources in charts.

 

Best regards,

Mikhail Brinchuk.

Aug 24, 2009 at 11:16 AM
Edited Aug 24, 2009 at 11:16 AM

Have you any update on this? I can change the line colour on the plotter but the legend item remains the same...

It would be a nice feature to have.

 

Regards,

quinlivan

 

Editor
Aug 24, 2009 at 4:49 PM

Hi,

of course, it would be a nice feature, but I have unfortunately only a couple of hands. I'm creating now a new Legend, that will use WPF's binding for being notified about such changes. But as this is not a primary goal of D3's improvement. this feature will appear a little later.

Sorry for making you wait.

Mikhail.

Aug 24, 2009 at 4:51 PM

Hi Mikhail

I will look forward to seeing the new Legend when released.

Thanks again for all your help

 

Editor
Aug 24, 2009 at 6:26 PM

Hi,

well, it took not very much time to create a quick and dirty fix for this issue. Now LegendItem for LineGraph reflects changes of parent's Stroke and StrokeThickness property.

Tomorrow I'll upload this code.

Best regards,

Mikhail.

Editor
Aug 25, 2009 at 9:17 AM

Hi,

I've created a quick fix, so now legend item for line chart reflects changes of Stroke and StrokeThickness of LineChart.

To demonstrate this feature, I've also created a small application with editing of these properties on-the-fly - it is called LineChartLegendBinding and its in the latest changeset, Nightly branch, DevSamples solution folder.

Best regards,

Mikhail.

Aug 25, 2009 at 11:24 AM

Excellent!

 

Works perfectly. Thank you so much - I promise I wont post/annoy you with any more questions