Skip to content

Commit 7bbccab

Browse files
committed
- (breaking changes) removed series types in Highchart control, now it's separated classes;
- other minor changes;
1 parent 65bc61b commit 7bbccab

18 files changed

+611
-421
lines changed

Controls/Highcharts/DataCollection.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
using System;
2-
3-
namespace Highcharts
1+
namespace Highcharts
42
{
3+
using System;
54
using System.Collections;
65
using System.Collections.Generic;
76

87
public class DataCollection : IEnumerable
98
{
109
private static readonly double[] emptyArray = new double[0];
11-
private readonly Series owner;
1210

13-
private double[] items = new double[0];
11+
private readonly Series owner;
12+
13+
private double[] items = new double[0]; // TODO: nullable type.
1414
private double max;
1515
private double min;
1616
private int size;

Controls/Highcharts/Highchart.cs

Lines changed: 80 additions & 221 deletions
Large diffs are not rendered by default.

Controls/Highcharts/LegendButton.cs

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@
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
}

Controls/Highcharts/PlotOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
{
33
public class PlotOptions
44
{
5+
public bool linearGradient { get; set; }
6+
public object linearGradientMaterial { get; set; } // Gradient material for areaSolidOutline series type.
57
}
68
}

Controls/Highcharts/Series.cs

Lines changed: 57 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,24 @@
33
using System;
44
using System.Drawing;
55

6-
public enum SeriesTypes
6+
public abstract class Series : IDisposable
77
{
8-
None = 0,
8+
internal SeriesCollection collection;
9+
internal readonly Pen pen = new Pen(Color.Transparent);
910

10-
areaSolid = 1,
11-
areaSolidOutline = 2,
12-
line = 3,
13-
lineSolid = 4,
14-
point = 5,
15-
}
11+
private string _name;
1612

17-
public class Series : IDisposable
18-
{
19-
internal SeriesCollection owner;
20-
internal readonly Pen pen = new Pen(Color.Transparent);
21-
internal bool _needRedraw = true;
13+
protected Series() : this("")
14+
{ }
2215

23-
private string _name;
24-
private bool _linearGradient;
25-
private float _pointInterval;
26-
private SeriesTypes _type;
27-
private bool _visible;
28-
29-
internal Series()
16+
protected Series(string name)
3017
{
3118
color = Color.Gray;
3219
data = new DataCollection(this);
3320
pointInterval = 1;
34-
type = SeriesTypes.line;
3521
visible = true;
36-
}
37-
internal Series(string n) : this()
38-
{
39-
name = n;
22+
23+
this.name = name;
4024
}
4125

4226
public event EventHandler NameChanged;
@@ -47,18 +31,7 @@ public Color color
4731
set { pen.Color = value; }
4832
}
4933
public DataCollection data { get; private set; }
50-
public bool linearGradient
51-
{
52-
get { return _linearGradient; }
53-
set
54-
{
55-
if (_linearGradient == value)
56-
return;
57-
58-
_linearGradient = value;
59-
_needRedraw = true;
60-
}
61-
}
34+
public int index { get; internal set; }
6235
public string name
6336
{
6437
get { return _name; }
@@ -76,55 +49,69 @@ public string name
7649
/// For example, if a series contains one value every decade starting from year 0, set pointInterval to 10.
7750
/// Defaults to 1.
7851
/// </summary>
79-
public float pointInterval
52+
public float pointInterval { get; set; }
53+
public object tag { get; set; }
54+
public bool visible { get; set; }
55+
public int yAxis { get; set; }
56+
57+
public void Dispose()
8058
{
81-
get { return _pointInterval; }
82-
set
83-
{
84-
if (_pointInterval == value)
85-
return;
59+
if (pen != null) pen.Dispose();
60+
}
8661

87-
_pointInterval = value;
88-
_needRedraw = true;
89-
}
62+
public override string ToString()
63+
{
64+
return "{name=" + name + "}";
9065
}
91-
public object tag { get; set; }
92-
public SeriesTypes type
66+
67+
internal void Paint(Graphics g, Rectangle clipRectangle)
9368
{
94-
get { return _type; }
95-
set
96-
{
97-
if (_type == value)
98-
return;
69+
if (!visible || data.Count == 0 || pointInterval <= 0) return;
70+
71+
OnPaint(g, clipRectangle);
72+
}
9973

100-
_type = value;
101-
_needRedraw = true;
102-
}
74+
internal void PaintIcon(Graphics g, Rectangle clipRectangle)
75+
{
76+
OnPaintIcon(g, clipRectangle);
10377
}
104-
public bool visible
78+
79+
public double GetValueRange()
10580
{
106-
get { return _visible; }
107-
set
108-
{
109-
if (_visible == value)
110-
return;
111-
112-
_visible = value;
113-
_needRedraw = true;
114-
}
81+
return collection.chart.cachedPlotMax - collection.chart.cachedPlotMin;
11582
}
116-
public int yAxis { get; set; }
11783

118-
public void Dispose()
84+
public float GetXFromIndex(float dataIndex, float xStep, Rectangle clipRectangle)
11985
{
120-
if (pen != null) pen.Dispose();
86+
return (float) Math.Ceiling(clipRectangle.Left + xStep * dataIndex);
87+
}
88+
89+
public float GetXStep()
90+
{
91+
var chartDataAmount = collection.chart.GetSeriesMaximumDataAmount();
92+
if (chartDataAmount < 1)
93+
return collection.chart.cachedPlotWidth / 2f; // Middle point of the plot.
94+
95+
return (float) collection.chart.cachedPlotWidth / (chartDataAmount - 1);
12196
}
12297

98+
public float GetYFromValue(double value, double range, Rectangle clipRectangle)
99+
{
100+
return (float) Math.Ceiling(clipRectangle.Bottom - clipRectangle.Height * GetYModFromValue(value, range));
101+
}
102+
103+
public float GetYModFromValue(double value, double range)
104+
{
105+
return (float) ((value - collection.chart.cachedPlotMin) / range);
106+
}
107+
123108
protected virtual void OnNameChanged(EventArgs e)
124109
{
125110
var handler = NameChanged;
126111
if (handler != null)
127112
handler(this, e);
128113
}
114+
protected abstract void OnPaint(Graphics g, Rectangle clipRectangle);
115+
protected abstract void OnPaintIcon(Graphics g, Rectangle clipRectangle);
129116
}
130-
}
117+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace Highcharts
2+
{
3+
using System.Drawing;
4+
5+
public class SeriesAreaSolid : Series
6+
{
7+
public SeriesAreaSolid() : this("")
8+
{ }
9+
10+
public SeriesAreaSolid(string name) : base(name)
11+
{ }
12+
13+
protected override void OnPaint(Graphics g, Rectangle clipRectangle)
14+
{
15+
if (data.Count == 1)
16+
{
17+
SeriesPaint.PaintPoint(this, g, clipRectangle, 0);
18+
return;
19+
}
20+
21+
var valueRange = GetValueRange();
22+
var xStep = GetXStep();
23+
var areaColorAlpha = collection.GetColorAlpha();
24+
var areaColor = Color.FromArgb(areaColorAlpha, color);
25+
26+
var prevValue = data[0];
27+
var prevValueX = GetXFromIndex(0, xStep, clipRectangle);
28+
var prevValueY = GetYFromValue(prevValue, valueRange, clipRectangle);
29+
30+
for (float dataIterator = pointInterval; dataIterator < data.Count; dataIterator += pointInterval)
31+
{
32+
double currentValue = data[(int) dataIterator];
33+
34+
float currentValueX = GetXFromIndex(dataIterator, xStep, clipRectangle);
35+
float currentValueY = GetYFromValue(currentValue, valueRange, clipRectangle);
36+
37+
float width = currentValueX - prevValueX;
38+
39+
g.uwfFillRectangle(areaColor, prevValueX, prevValueY, width, clipRectangle.Bottom - prevValueY);
40+
41+
if (dataIterator + pointInterval >= data.Count)
42+
g.uwfFillRectangle(areaColor, currentValueX, currentValueY, xStep, clipRectangle.Bottom - currentValueY);
43+
44+
prevValueX = currentValueX;
45+
prevValueY = currentValueY;
46+
47+
SeriesPaint.PaintHoveredValueRect(this, g, currentValueX, currentValueY, xStep);
48+
}
49+
}
50+
51+
protected override void OnPaintIcon(Graphics g, Rectangle clipRectangle)
52+
{
53+
g.uwfFillRectangle(color, clipRectangle.Left + 4, clipRectangle.Top + 4, 8, 8);
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)