Recent

Author Topic: Dynamically adding/removing TChartStye  (Read 2998 times)

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Dynamically adding/removing TChartStye
« on: January 23, 2017, 04:38:04 pm »
I'm drawing a stacked bat chart, with a single bar and a variable number of "slices". So a bit like a pie chart but as a vertical rectangle. I find I need to dynamically add TChartStyle items to the TChartStyles component I am using. This seems necessary ij order to get the legend having the correct number of entries. My problem is that I cannot find the relevant "Clear" and "Add" methods for the TChartStyles. Each time the chart is drawn I want to delete all styles from the TChartStyles and then add a TChartStyle for eacj slice that I am adding to the bar. Can you tell me the methods to do this please?

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Dynamically adding/removing TChartStye
« Reply #1 on: January 23, 2017, 06:38:43 pm »
The TChartStyles component houses a collection named Styles which contains all styles. Therefore, you simply call ChartStyles1.Styles.Add to create a new ChartStyle and add it to the collection. The Add method returns a pointer to the newly created collectionitem; you must cast it TChartStyle to get access to its properties. In order to delete a style just call the Delete method of the collection with the index of the corresponding style (ChartStyles1.Styles.Delete(index)):
Code: Pascal  [Select][+][-]
  1. var
  2.   style: TChartStyle;
  3. ...
  4.   style := TChartStyle(ChartStyles1.Styles.Add);
  5.   style.Brush.Color := clBlue;
  6.   style.Text := 'Some legend text';
  7. ...
  8.   ChartStyles1.Styles.Delete(index_to_be_deleted);

In the attachment, there's a little demo which adds stacked bars at runtime and allows deletion of the stack level selected in a listbox.

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: Dynamically adding/removing TChartStye
« Reply #2 on: January 23, 2017, 11:34:26 pm »
Thanks for that. All working fine now.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Dynamically adding/removing TChartStye
« Reply #3 on: January 24, 2017, 12:56:21 pm »
I added the demo to the examples coming with TAChart (tachart/demo/runtime/chartstyles) so that it won't get buried in the forum. Probably more to follow in the runtime folder...

 

TinyPortal © 2005-2018