Imports System.Collections.Generic
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms
Imports Spread.Common.DataStore.Models
Imports Spread.Common.DataStore
Imports FarPoint.Win.Spread
Imports FarPoint.Win.Spread.Chart
Imports Spread.Common.Features.Spread.Common.Features.Sample
Imports Spread.Sparkline.Spread.Sparkline

Namespace Features
    Partial Public Class CustomSparkline
        Inherits BaseSampleControl
        Private _sparklineGroup As BulletSparklineGroup

        Public Sub New()
            InitializeComponent()
            Name = "Custom Sparkline"

            comboOrientation.SelectedIndex = 0
            btnColor.BackColor = Color.LightSteelBlue

            fpSpread.ActiveSheet.DataSource = GetDataTable()

            AddSparklineColumn(fpSpread.ActiveSheet)
            SetSheetProperties(fpSpread.ActiveSheet)

            AddHandler btnChange.Click, AddressOf BtnChange_Click
            AddHandler btnColor.Click, AddressOf BtnColor_Click
        End Sub

        Private Sub SetSheetProperties(sheet As SheetView)
            sheet.Columns(sheet.ColumnCount - 1).Width = 120
            sheet.SetMultipleRowHeights(0, sheet.RowCount, 24)
        End Sub

        Private Sub AddSparklineColumn(sheet As SheetView)
            sheet.AddColumns(sheet.ColumnCount, 1)
            Dim col As Integer = sheet.ColumnCount - 1

            _sparklineGroup = New BulletSparklineGroup()
            Dim setting As New BulletSparklineSetting(10, Color.LightSteelBlue, False)
            _sparklineGroup.Setting = setting
            _sparklineGroup.Renderer = New BulletSparklineRenderer()

            sheet.SparklineContainer.Add(_sparklineGroup)

            For row As Integer = 0 To sheet.RowCount - 1
                _sparklineGroup.Add(New Spread.Sparkline.BulletSparkline(sheet, row, col, New SheetCellRange(sheet, row, 1, 1, 1), New SheetCellRange(sheet, row, 2, 1, 1), New SheetCellRange(sheet, row, 3, 1, 1),
                    New SheetCellRange(sheet, row, 5, 1, 1), New SheetCellRange(sheet, row, 4, 1, 1), New SheetCellRange(sheet, row, 6, 1, 1)))
            Next
        End Sub

        Private Function GetDataTable() As DataTable
            Dim students As List(Of Student) = New DataService().StudentDashboardDataSource.GetAllStudents()
            Dim table As New DataTable()
            table.Columns.Add("StudentName", GetType(String))
            table.Columns.Add("Score", GetType(Integer))
            table.Columns.Add("Target", GetType(Integer))
            table.Columns.Add("Forecast", GetType(Integer))
            table.Columns.Add("Bad", GetType(Integer))
            table.Columns.Add("Good", GetType(Integer))
            table.Columns.Add("Maximum", GetType(Integer))

            Dim random As New Random()
            For Each student As Student In students
                table.Rows.Add(student.Name, random.[Next](30, 100), random.[Next](60, 90), random.[Next](50, 80), 40, 60,
                    100)
            Next
            Return table
        End Function

        Private Sub BtnColor_Click(sender As Object, e As EventArgs)
            Dim dialog As New ColorDialog()
            If dialog.ShowDialog() = DialogResult.OK Then
                btnColor.BackColor = dialog.Color
            End If
        End Sub

        Private Sub BtnChange_Click(sender As Object, e As EventArgs)
            Dim setting As New BulletSparklineSetting()
            setting.Vertical = (comboOrientation.SelectedItem.ToString() = "Vertical")
            setting.ColorScheme = btnColor.BackColor
            setting.TickUnit = CDbl(numericTickUnit.Value)

            _sparklineGroup.Setting = setting
        End Sub
    End Class
End Namespace


'
'CustomSparkline.Designer.vb
'


Namespace Features
	Partial Class CustomSparkline
		''' <summary> 
		''' Required designer variable.
		''' </summary>
		Private components As System.ComponentModel.IContainer = Nothing

        ''' <summary> 
        ''' Clean up any resources being used.
        ''' </summary>
        ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        Protected Shadows Sub Dispose(disposing As Boolean)
            If disposing AndAlso (components IsNot Nothing) Then
                components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub

#Region "Component Designer generated code"

        ''' <summary> 
        ''' Required method for Designer support - do not modify 
        ''' the contents of this method with the code editor.
        ''' </summary>
        Private Sub InitializeComponent()
			Me.fpSpread = New FarPoint.Win.Spread.FpSpread()
			Me.fpSpread_Sheet1 = New FarPoint.Win.Spread.SheetView()
			Me.groupBox1 = New System.Windows.Forms.GroupBox()
			Me.btnChange = New System.Windows.Forms.Button()
			Me.label3 = New System.Windows.Forms.Label()
			Me.btnColor = New System.Windows.Forms.Button()
			Me.label2 = New System.Windows.Forms.Label()
			Me.comboOrientation = New System.Windows.Forms.ComboBox()
			Me.label1 = New System.Windows.Forms.Label()
			Me.numericTickUnit = New System.Windows.Forms.NumericUpDown()
			Me.pnlMain.SuspendLayout()
			Me.pnlControls.SuspendLayout()
			DirectCast(Me.fpSpread, System.ComponentModel.ISupportInitialize).BeginInit()
			DirectCast(Me.fpSpread_Sheet1, System.ComponentModel.ISupportInitialize).BeginInit()
			Me.groupBox1.SuspendLayout()
			DirectCast(Me.numericTickUnit, System.ComponentModel.ISupportInitialize).BeginInit()
			Me.SuspendLayout()
			' 
			' pnlMain
			' 
			Me.pnlMain.Controls.Add(Me.fpSpread)
			' 
			' pnlControls
			' 
			Me.pnlControls.Controls.Add(Me.groupBox1)
			' 
			' fpSpread
			' 
			Me.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill
			Me.fpSpread.Location = New System.Drawing.Point(0, 0)
			Me.fpSpread.Name = "fpSpread"
			Me.fpSpread.Sheets.AddRange(New FarPoint.Win.Spread.SheetView() {Me.fpSpread_Sheet1})
			Me.fpSpread.Size = New System.Drawing.Size(477, 487)
			Me.fpSpread.TabIndex = 0
			' 
			' fpSpread_Sheet1
			' 
			Me.fpSpread_Sheet1.Reset()
			Me.fpSpread_Sheet1.SheetName = "Sheet1"
			' Formulas and custom names must be loaded with R1C1 reference style
			Me.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1
			Me.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1
			' 
			' groupBox1
			' 
			Me.groupBox1.Controls.Add(Me.numericTickUnit)
			Me.groupBox1.Controls.Add(Me.btnChange)
			Me.groupBox1.Controls.Add(Me.label3)
			Me.groupBox1.Controls.Add(Me.btnColor)
			Me.groupBox1.Controls.Add(Me.label2)
			Me.groupBox1.Controls.Add(Me.comboOrientation)
			Me.groupBox1.Controls.Add(Me.label1)
			Me.groupBox1.Dock = System.Windows.Forms.DockStyle.Top
			Me.groupBox1.Location = New System.Drawing.Point(0, 0)
			Me.groupBox1.Name = "groupBox1"
			Me.groupBox1.Size = New System.Drawing.Size(157, 171)
			Me.groupBox1.TabIndex = 0
			Me.groupBox1.TabStop = False
			Me.groupBox1.Text = "Sparkline Setting"
			' 
			' btnChange
			' 
			Me.btnChange.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.btnChange.Location = New System.Drawing.Point(28, 132)
			Me.btnChange.Name = "btnChange"
			Me.btnChange.Size = New System.Drawing.Size(104, 23)
			Me.btnChange.TabIndex = 6
			Me.btnChange.Text = "Change Settings"
			Me.btnChange.UseVisualStyleBackColor = True
			' 
			' label3
			' 
			Me.label3.AutoSize = True
			Me.label3.Location = New System.Drawing.Point(7, 99)
			Me.label3.Name = "label3"
			Me.label3.Size = New System.Drawing.Size(53, 13)
			Me.label3.TabIndex = 4
			Me.label3.Text = "Tick Unit:"
			' 
			' btnColor
			' 
			Me.btnColor.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.btnColor.Location = New System.Drawing.Point(74, 60)
			Me.btnColor.Name = "btnColor"
			Me.btnColor.Size = New System.Drawing.Size(77, 23)
			Me.btnColor.TabIndex = 3
			Me.btnColor.UseVisualStyleBackColor = True
			' 
			' label2
			' 
			Me.label2.AutoSize = True
			Me.label2.Location = New System.Drawing.Point(7, 65)
			Me.label2.Name = "label2"
			Me.label2.Size = New System.Drawing.Size(34, 13)
			Me.label2.TabIndex = 2
			Me.label2.Text = "Color:"
			' 
			' comboOrientation
			' 
			Me.comboOrientation.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.comboOrientation.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
			Me.comboOrientation.FormattingEnabled = True
			Me.comboOrientation.Items.AddRange(New Object() {"Horizontal", "Vertical"})
			Me.comboOrientation.Location = New System.Drawing.Point(74, 26)
			Me.comboOrientation.Name = "comboOrientation"
			Me.comboOrientation.Size = New System.Drawing.Size(77, 21)
			Me.comboOrientation.TabIndex = 1
			' 
			' label1
			' 
			Me.label1.AutoSize = True
			Me.label1.Location = New System.Drawing.Point(7, 29)
			Me.label1.Name = "label1"
			Me.label1.Size = New System.Drawing.Size(61, 13)
			Me.label1.TabIndex = 0
			Me.label1.Text = "Orientation:"
			' 
			' numericTickUnit
			' 
			Me.numericTickUnit.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.numericTickUnit.Location = New System.Drawing.Point(74, 97)
			Me.numericTickUnit.Maximum = New Decimal(New Integer() {10, 0, 0, 0})
			Me.numericTickUnit.Name = "numericTickUnit"
			Me.numericTickUnit.Size = New System.Drawing.Size(77, 20)
			Me.numericTickUnit.TabIndex = 7
			Me.numericTickUnit.Value = New Decimal(New Integer() {10, 0, 0, 0})
			' 
			' CustomSparkline
			' 
			Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
			Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
			Me.Name = "CustomSparkline"
			Me.pnlMain.ResumeLayout(False)
			Me.pnlControls.ResumeLayout(False)
			DirectCast(Me.fpSpread, System.ComponentModel.ISupportInitialize).EndInit()
			DirectCast(Me.fpSpread_Sheet1, System.ComponentModel.ISupportInitialize).EndInit()
			Me.groupBox1.ResumeLayout(False)
			Me.groupBox1.PerformLayout()
			DirectCast(Me.numericTickUnit, System.ComponentModel.ISupportInitialize).EndInit()
			Me.ResumeLayout(False)

		End Sub

		#End Region

		Private fpSpread As FarPoint.Win.Spread.FpSpread
		Private fpSpread_Sheet1 As FarPoint.Win.Spread.SheetView
		Private groupBox1 As System.Windows.Forms.GroupBox
		Private label3 As System.Windows.Forms.Label
		Private btnColor As System.Windows.Forms.Button
		Private label2 As System.Windows.Forms.Label
		Private comboOrientation As System.Windows.Forms.ComboBox
		Private label1 As System.Windows.Forms.Label
		Private btnChange As System.Windows.Forms.Button
		Private numericTickUnit As System.Windows.Forms.NumericUpDown
	End Class
End Namespace
