66
77 internal sealed class LegendButton : Button
88 {
9- internal bool seriesEnabled ;
10-
11- private readonly Pen seriesPen = new Pen ( Color . Transparent , 2 ) ;
9+ private const int DefaultTextMarginLeft = 16 ;
1210
1311 public LegendButton ( Legend l , Series s )
1412 {
1513 Legend = l ;
1614 Series = s ;
17- Text = s . name ;
18-
19- OnMouseClick ( new MouseEventArgs ( MouseButtons . Left , 1 , 0 , 0 , 0 ) ) ;
15+ TextLeftMargin = DefaultTextMarginLeft ;
16+
17+ TextFromSeries ( ) ;
2018 }
2119
2220 public Legend Legend { get ; private set ; }
2321 public Series Series { get ; private set ; }
22+ public int TextLeftMargin { get ; set ; }
2423
2524 protected override void OnMouseClick ( MouseEventArgs e )
2625 {
@@ -29,11 +28,13 @@ protected override void OnMouseClick(MouseEventArgs e)
2928 switch ( e . Button )
3029 {
3130 case MouseButtons . Left :
32- seriesEnabled = ! seriesEnabled ;
31+ Series . visible = ! Series . visible ;
3332
34- Series . visible = seriesEnabled ;
35- if ( Parent != null ) ( ( Highchart ) Parent ) . RecalcCategories ( ) ;
33+ var highcharts = Parent as Highchart ;
34+ if ( highcharts != null )
35+ highcharts . UpdatePlot ( ) ; // Update plot min and max values range.
3636 break ;
37+
3738 case MouseButtons . Right :
3839
3940 var itemColor = new ToolStripMenuItem ( Highchart . textLegendMenu_Color ) ;
@@ -44,33 +45,23 @@ protected override void OnMouseClick(MouseEventArgs e)
4445 form . ColorChanged += ( sender2 , args2 ) => Series . color = form . Color ;
4546 form . ShowDialog ( ) ;
4647 } ;
47-
48- var itemType = new ToolStripMenuItem ( Highchart . textLegendMenu_Type ) ;
49-
50- var seriesTypes = Highchart . textLegendMenu_TypeNames ;
51- for ( int i = 0 ; i < seriesTypes . Length ; i ++ )
52- {
53- int index = i ;
54- var item = new ToolStripMenuItem ( seriesTypes [ i ] ) ;
55- item . Click += ( s , a ) => { Series . type = ( SeriesTypes ) index ; } ;
56- itemType . DropDownItems . Add ( item ) ;
57- }
5848
5949 var context = new ContextMenuStrip ( ) ;
6050 context . Items . Add ( itemColor ) ;
61- context . Items . Add ( itemType ) ;
6251 context . Show ( null , MousePosition ) ;
6352 break ;
6453 }
6554 }
55+
6656 protected override void OnPaint ( PaintEventArgs e )
6757 {
6858 var graphics = e . Graphics ;
6959
7060 // User original series color or hidden color for icon and use styles for text.
7161 var seriesColor = Series . color ;
7262 var textColor = Legend . itemStyle . color ;
73- if ( seriesEnabled == false )
63+
64+ if ( ! Series . visible )
7465 {
7566 seriesColor = Legend . itemHiddenStyle . color ;
7667 textColor = seriesColor ;
@@ -80,30 +71,19 @@ protected override void OnPaint(PaintEventArgs e)
8071 if ( hovered )
8172 textColor = Legend . itemHoverStyle . color ;
8273
83- switch ( Series . type )
84- {
85- case SeriesTypes . areaSolid :
86- case SeriesTypes . areaSolidOutline :
87- graphics . uwfFillRectangle ( seriesColor , 4 , 8 , 8 , 8 ) ;
88- break ;
89- case SeriesTypes . line :
90- seriesPen . Color = seriesColor ;
91- graphics . DrawLine ( seriesPen , 0 , 11 , 16 , 11 ) ;
92- break ;
93- case SeriesTypes . lineSolid :
94- seriesPen . Color = seriesColor ;
95- graphics . DrawLine ( seriesPen , 0 , 11 , 16 , 11 ) ;
96- graphics . uwfDrawImage ( ApplicationResources . Images . Circle , seriesColor , 4 , 8 , 8 , 8 ) ;
97- break ;
98- case SeriesTypes . point :
99- graphics . uwfDrawImage ( ApplicationResources . Images . Circle , seriesColor , 4 , 8 , 8 , 8 ) ;
100- break ;
101- }
74+ Series . PaintIcon ( graphics , new Rectangle ( 0 , 0 , TextLeftMargin , 16 ) ) ;
10275
103- graphics . uwfDrawString ( Text , Font , textColor , 16 , 0 , Width - 16 , Height , ContentAlignment . MiddleCenter ) ;
76+ graphics . uwfDrawString ( Text , Font , textColor , TextLeftMargin , 0 , Width - TextLeftMargin , Height , ContentAlignment . MiddleCenter ) ;
10477 }
105- protected override void OnPaintBackground ( PaintEventArgs pevent )
78+
79+ protected override void OnPaintBackground ( PaintEventArgs e )
80+ { }
81+
82+ private void TextFromSeries ( )
10683 {
84+ Text = Series . name != null
85+ ? Series . name
86+ : "Series " + ( Series . index + 1 ) ;
10787 }
10888 }
10989}
0 commit comments