Home 소개       다운로드       온라인 설명서      주식차트    Q & A     Blog    

히포차트로 추세선 그리기







추세선 공식을 응용하여 히포차트로 추세선 그리는 방법을 알아봅니다.




히포차트로 추세선 그리기  히포차트4 - n차 추세선 그래프  


C#
 
private void draw()
{
SeriesList sList = new SeriesList();
sList.ChartType = ChartType.Scatter;

Random r = new Random();
for(int i = 0; i < 1; i++)
{
Series sr = new Series();
sr.Points.Width = 4;
sr.Points.PointType = PointType.FillCircle;

for(int x = 0; x < 150; x++)
{
SeriesItem item = new SeriesItem();
item.XValue = x;

item.YValue = r.Next(500) * x + 13;

sr.items.Add(item);
}
sList.SeriesCollection.Add(sr);
}

sList.SeriesCollection.Add(this.GetTrendSeries(sList));


this.hHippoChart2.LegendBox.Visible = false;
this.hHippoChart2.Titles.Label.ForeColor = Color.SteelBlue;
this.hHippoChart2.DesignType = ChartDesignType.Simple;
this.hHippoChart2.SeriesListDictionary.Add(sList);
this.hHippoChart2.DrawChart();
}

private Series GetTrendSeries(SeriesList sList)
{
Series TrSr = new Series();
TrSr.SeriesColor = Color.Blue;
TrSr.ChartType = ChartType.Line;

double a = 0;
double b = 0;

double[,] MatrixL = new double[2, 2];
double[,] InverseMatrixL = new double[2, 2];
double[] MatrixR = new double[2];

int totalcount = 0;

foreach(Series sr in sList.SeriesCollection)
{
foreach(SeriesItem item in sr.items)
{
MatrixL[0, 0] = MatrixL[0, 0] + 1;
MatrixL[1, 0] += item.XValue * 1;
MatrixL[0, 1] += item.XValue * 1;
MatrixL[1, 1] += item.XValue * item.XValue;

MatrixR[0] += item.YValue * 1;
MatrixR[1] += item.YValue * item.XValue;

totalcount++;
}
}

// 역행렬을 만드는 공식에서 ad - bc 체크
double adMinusbc = MatrixL[0, 0] * MatrixL[1, 1] - MatrixL[1, 0] * MatrixL[0, 1];

if (adMinusbc != 0)
{
InverseMatrixL[0, 0] = MatrixL[1, 1] / adMinusbc;
InverseMatrixL[1, 0] = (MatrixL[1, 0] * (-1)) / adMinusbc;
InverseMatrixL[0, 1] = (MatrixL[0, 1] * (-1)) / adMinusbc;
InverseMatrixL[1, 1] = MatrixL[0, 0] / adMinusbc;

a = (InverseMatrixL[0, 0] * MatrixR[0] + InverseMatrixL[1, 0] * MatrixR[1]);
b = (InverseMatrixL[0, 1] * MatrixR[0] + InverseMatrixL[1, 1] * MatrixR[1]);

// 해당 시리즈 만들기
for(int i = 0; i < totalcount; i++)
{
SeriesItem tritem = new SeriesItem();
tritem.XValue = i;
tritem.YValue = (float)(b * i + a);

TrSr.items.Add(tritem);
}
}
else // 나누는 분모가 0이 되므로 불능
{
// 해당 시리즈 만들기
for(int i = 0; i < totalcount; i++)
{
SeriesItem tritem = new SeriesItem();
tritem.XValue = i;
tritem.YValue = 0;

TrSr.items.Add(tritem);
}
}

return TrSr;
}


VB
 
Private Sub draw()
Dim sList As New. SeriesList()
sList.ChartType = ChartType.Scatter

Dim r As New. Random()
Dim i As Integer = 0
While i < 1
Dim sr As New. Series()
sr.Points.Width = 4
sr.Points.PointType = PointType.FillCircle

Dim x As Integer = 0
While x < 150
Dim item As New. SeriesItem()
item.XValue = x

item.YValue = r.[Next](500) * x + 13

sr.items.Add(item)
System.Math.Max(System.Threading.Interlocked.Increment(x),x - 1)
End While
sList.SeriesCollection.Add(sr)
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While

sList.SeriesCollection.Add(Me.GetTrendSeries(sList))


Me.hHippoChart2.LegendBox.Visible = False
Me.hHippoChart2.Titles.Label.ForeColor = Color.SteelBlue
Me.hHippoChart2.DesignType = ChartDesignType.Simple
Me.hHippoChart2.SeriesListDictionary.Add(sList)
Me.hHippoChart2.DrawChart()
End Sub

Private Function GetTrendSeries(sList As SeriesList) As Series
Dim TrSr As New. Series()
TrSr.SeriesColor = Color.Blue
TrSr.ChartType = ChartType.Line

Dim a As Double = 0
Dim b As Double = 0

Dim MatrixL As Double(,) = New Double(2, 2) {}
Dim InverseMatrixL As Double(,) = New Double(2, 2) {}
Dim MatrixR As Double() = New Double(2) {}

Dim totalcount As Integer = 0

For Each sr As Series In sList.SeriesCollection
For Each item As SeriesItem In sr.items
MatrixL(0, 0) = MatrixL(0, 0) + 1
MatrixL(1, 0) += item.XValue * 1
MatrixL(0, 1) += item.XValue * 1
MatrixL(1, 1) += item.XValue * item.XValue

MatrixR(0) += item.YValue * 1
MatrixR(1) += item.YValue * item.XValue

System.Math.Max(System.Threading.Interlocked.Increment(totalcount),totalcount - 1)
Next
Next

` 역행렬을 만드는 공식에서 ad - bc 체크
Dim adMinusbc As Double = MatrixL(0, 0) * MatrixL(1, 1) - MatrixL(1, 0) * MatrixL(0, 1)

If adMinusbc <> 0 Then
InverseMatrixL(0, 0) = MatrixL(1, 1) / adMinusbc
InverseMatrixL(1, 0) = (MatrixL(1, 0) * (-1)) / adMinusbc
InverseMatrixL(0, 1) = (MatrixL(0, 1) * (-1)) / adMinusbc
InverseMatrixL(1, 1) = MatrixL(0, 0) / adMinusbc

a = (InverseMatrixL(0, 0) * MatrixR(0) + InverseMatrixL(1, 0) * MatrixR(1))
b = (InverseMatrixL(0, 1) * MatrixR(0) + InverseMatrixL(1, 1) * MatrixR(1))

` 해당 시리즈 만들기
Dim i As Integer = 0
While i < totalcount
Dim tritem As New. SeriesItem()
tritem.XValue = i
tritem.YValue = DirectCast((b * i + a), Single)

TrSr.items.Add(tritem)
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
Else
` 나누는 분모가 0이 되므로 불능
` 해당 시리즈 만들기
Dim i As Integer = 0
While i < totalcount
Dim tritem As New. SeriesItem()
tritem.XValue = i
tritem.YValue = 0

TrSr.items.Add(tritem)
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
End If

Return TrSr
End Function




※ 히포차트 샘플 코드는 버전별로 상이한 결과를 나타낼 수 있습니다.

이 코드 관련 문의 사항은 페이스북 리플을 달아주시거나 아래 이메일로 이 페이지 주소와 함께 문의주세요.

helpdesk@hippochart.com





Copyright © 2009-2016, HippoChart. Inc.  All Rights Reserved.