Silverlightベースのチャートコントロールからプラグインレスのコントロールに移行しようとしています。 HighChartsが好きで、既存のコードベースへの実装をテストしています。 DotNet.HighChartsは、すべてのjavascriptを解析することなく、迅速にチャートを作成できる可能性があると判明しました。DotNet.HighChartsライブラリを使用してWebサービスからのDataTable結果に基づくグラフを作成する
私の質問は、DataTable
に含まれている場合、チャートで使用するデータをどのように渡しますか?答えがhereに続いて、私はLinqが行く方法かも知れません。しかし、私はコードを実際にビルドするのは苦労しています。 DotNet.HighChartsのドキュメントには非静的なデータを取得するための例は表示されていません。これは、これまでの私のコードの抜粋です:
Dim da3 As New SqlDataAdapter(sql3, conn3)
da3.Fill(dt3)
da3.Dispose()
conn3.Close()
Dim chart3 As Highcharts = New Highcharts("chart").InitChart(New Chart() With { _
.PlotShadow = False _
}).SetTitle(New Title() With { _
.Text = "Browser market shares at a specific website, 2010" _
}).SetTooltip(New Tooltip() With { _
.Formatter = "function() { return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; }" _
}).SetPlotOptions(New PlotOptions() With { _
.Column = New PlotOptionsColumn With { _
.AllowPointSelect = True, _
.Cursor = Cursors.Pointer, _
.DataLabels = New PlotOptionsColumnDataLabels() With { _
.Color = ColorTranslator.FromHtml("#000000"), _
.Formatter = "function() { return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; }" _
} _
} _
}).SetSeries(New Series() With { _
.Type = ChartTypes.Column, _
.Name = "Browser share", _
.Data = New Helpers.Data(dt3.Select(Function(x) New Options.Point() With {.X = x.periodyear, .Y = x.rate}).ToArray()) _
})
ltrChart3.Text = chart3.ToHtmlString()
私は次のようにインポートしています:
Imports DotNet.Highcharts
Imports DotNet.Highcharts.Options
Imports System.Data
Imports System.Data.SqlClient
Imports DotNet.Highcharts.Enums
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
私が手にエラーが.Data = New Helpers.Data(dt3.Select(Function(x) New Options.Point() With {.X = x.periodyear, .Y = x.rate}).ToArray()) _
ライン上にあります。私はFunction....rate}
ビットで私のエラーとしてLambda expression cannot be converted to 'String' because 'String' is not a delegate type
を取得しています。
EDIT:既存のコードを修正することで 最初の試み
Dim chart1 As Highcharts = New DotNet.Highcharts.Highcharts("test")
Dim series As Series = New Series()
series.Name = "CO Rates"
For i As Integer = 0 To dt.Rows.Count - 1
series.Data = New Helpers.Data(New Object() {dt.Rows(i)("periodyear"), dt.Rows(i)("rate")})
Next
chart1.SetSeries(series).InitChart(New Chart() With { _
.Type = ChartTypes.Column})
これはちょうど最初のx位置0にある2つの列を生成し、その値2011であり、他方はx位置1にあり、その値は8.3です。これは、dataTableの最後の点を取って、その{x、y}の値({2011、8.3})を2つの異なる点にするように見えるので、非常に奇妙です.xとyの両方の値がy {0、x}や{1、y}のような値です。 私は取得する必要があります:
- {periodyear、rate}をdataTableから取得した単一のシリーズです。
- 将来的には、各場所の系列を、 {periodyear、rate}というdataTableから取得したいと考えています。
EDIT 2: さて、私はそれがデータ辞書に結果を変換すると、私はすべてのポイントを表示するには、チャートを取得しています持っています。今すぐ問題は、dt.Rows(i)("periodyear")
をDateTime値に変換することだけです。これは、これまで(PaseExact方法に失敗した)コードです:
Dim series As Series = New Series()
series.Name = "CO Rates"
Dim xDate As DateTime
Dim data As New Dictionary(Of DateTime, Decimal)
For i As Integer = 0 To dt.Rows.Count - 1
xDate = DateTime.ParseExact(dt.Rows(i)("periodyear").ToString, "YYYY", Globalization.CultureInfo.InvariantCulture)
data.Add(xDate, dt.Rows(i)("rate"))
Next
Dim chartData As Object(,) = New Object(data.Count - 1, 1) {}
Dim x As Integer = 0
For Each pair As KeyValuePair(Of DateTime, Decimal) In data
chartData.SetValue(pair.Key, x, 0)
chartData.SetValue(pair.Value, x, 1)
x += 1
Next
Dim chart1 As Highcharts = New Highcharts("chart1").InitChart(New Chart() With { _
.Type = ChartTypes.Column _
}).SetTitle(New Title() With { _
.Text = "Chart 1" _
}).SetXAxis(New XAxis() With { _
.Type = AxisTypes.Linear _
}).SetSeries(New Series() With { _
.Data = New Helpers.Data(chartData) _
})
私は(例えば、1980年のための)単なる文字列値にこれを変更して、私は、x軸用リニアにAxisTypesを変更した場合表示データ。何とかしていない - 1980年は1,980のように見える。ベビーステップ...
EDIT N: これは最終的な解決策です。これは、いくつかのクリーンアップを使用することができるようだ。たとえば、X/Y値を入力して辞書を繰り返して、chartDataオブジェクトにデータを追加する必要があるかどうかはわかりません。これは二重の仕事のようです。ここで
Dim stfipsList = (From r In dt.AsEnumerable() Select r("stfips")).Distinct().ToList()
Dim SeriesList As New List(Of Series)(stfipsList.Count)
Dim seriesItem(stfipsList.Count) As Series
Dim xDate As DateTime
Dim fakeDate As String
Dim sX As Integer
sX = 1
For Each state In stfipsList
Dim data As New Dictionary(Of DateTime, Decimal)
Dim stateVal As String = state.ToString
Dim recCount As Integer = dt.Rows.Count - 1
For i As Integer = 0 To recCount
If dt.Rows(i)("stfips").ToString = stateVal Then
fakeDate = "1/1/" + dt.Rows(i)("periodyear").ToString
xDate = DateTime.Parse(fakeDate)
data.Add(xDate.Date, dt.Rows(i)("unemprate"))
End If
Next
Dim chartData As Object(,) = New Object(data.Count - 1, 1) {}
Dim x As Integer = 0
For Each pair As KeyValuePair(Of DateTime, Decimal) In data
chartData.SetValue(pair.Key, x, 0)
chartData.SetValue(pair.Value, x, 1)
x += 1
Next
seriesItem(sX) = New Series With {
.Name = state.ToString, _
.Data = New Helpers.Data(chartData)
}
SeriesList.Add(seriesItem(sX))
sX = sX + 1
Next
Dim chart1 As Highcharts = New Highcharts("chart1").InitChart(New Chart() With { _
.Type = ChartTypes.Line _
}).SetTitle(New Title() With { _
.Text = "Annual Unemployment Rate" _
}).SetTooltip(New Tooltip() With { _
.Formatter = "function() { return '<b>'+ this.series.name + ': ' + Highcharts.dateFormat('%Y', this.x) +'</b>: '+ this.y +' %'; }" _
}).SetXAxis(New XAxis() With { _
.Type = AxisTypes.Datetime _
}).SetYAxis(New YAxis() With { _
.Min = 0, _
.Title = New YAxisTitle() With { _
.Text = "Unemployment Rate", _
.Align = AxisTitleAligns.High _
} _
}).SetSeries(SeriesList.[Select](Function(s) New Series() With { _
.Name = s.Name, _
.Data = s.Data _
}).ToArray())
ltrChart1.Text = chart1.ToHtmlString()
私が行っているこの種のディスカッションポストを見てください。これを試してみると、データポイントごとに異なるx個の系列が得られますか?私はプロットする複数のシリーズを持っているときにこのようにしたい理由があるのですが、結果セットの各行のグラフにperiodyearとrateの値を1つのセットとして渡したいだけです。私は投げているものがWith {}構文であると思います。私は自分のコードを深く見ていきます。私は私の質問を私の非実践的な試みで更新しました。 – wergeld