Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Linq
Imports FarPoint.Win.Spread
Imports FarPoint.Win.Spread.Model
Imports FarPoint.Win.Spread.CellType
Imports System.Drawing
Imports Spread.Common.Features.Spread.Common.Features.Sample
Imports Spread.Sparkline.Spread.Sparkline

Namespace Features
    Partial Public Class SparklineGroupsAndSettings
        Inherits BaseSampleControl

        Public Sub New()
            InitializeComponent()
            Name = "Sparkline Groups and Settings"
            InitSpreadSheet(fpSpread.ActiveSheet)
            InitControls()
            AddHandler fpSpread.SelectionChanged, AddressOf FpSpread_SelectionChanged
        End Sub

        Private Sub InitSpreadSheet(ByVal sheet As SheetView)
            fpSpread.AllowCellOverflow = True
            sheet.SetMultipleRowHeights(0, sheet.RowCount, 24)
            sheet.SetMultipleColumnWidths(0, sheet.ColumnCount, 75)
            sheet.OperationMode = OperationMode.Normal
            sheet.SelectionPolicy = SelectionPolicy.MultiRange
            sheet.Columns(6).Width = 120
            AddSampleSparklines(sheet)
        End Sub

        Private Sub AddSampleSparklines(ByVal sheet As SheetView)
            Dim boldFont As Font = New Font("Segoe", 9, FontStyle.Bold)
            sheet.Cells(4, 1).Text = "Sales Analysis example:"
            sheet.Cells(4, 1).Font = boldFont
            sheet.SetClip(5, 1, 1, 6, String.Join(vbTab, "Region", "Q1", "Q2", "Q3", "Q4", "Regional Sales"))
            sheet.Cells(5, 1, 5, 6).HorizontalAlignment = CellHorizontalAlignment.Center
            sheet.Cells(5, 1, 5, 6).Font = boldFont
            sheet.Cells(6, 1).Text = "East"
            sheet.Cells(8, 1).Text = "South"
            sheet.Cells(10, 1).Text = "North"
            sheet.Cells(12, 1).Text = "West"
            Dim random As Random = New Random()
            Dim currencyCell As CurrencyCellType = New CurrencyCellType()

            For row As Integer = 6 To 13 - 1 Step 2
                sheet.Cells(row, 2, row, 5).CellType = currencyCell

                For col As Integer = 2 To 6 - 1
                    sheet.Cells(row, col).Value = random.[Next](400, 1200)
                Next
            Next

            sheet.SparklineContainer.Add(New ExcelSparklineGroup(New ExcelSparklineSetting(), SparklineType.Line))
            sheet.SparklineContainer(0).Add(New ExcelSparkline(6, 6, sheet, New CellRange(6, 2, 1, 4)))
            sheet.SparklineContainer(0).Add(New ExcelSparkline(8, 6, sheet, New CellRange(8, 2, 1, 4)))
            sheet.SparklineContainer.Add(New ExcelSparklineGroup(New ExcelSparklineSetting(), SparklineType.Column))
            sheet.SparklineContainer(1).Add(New ExcelSparkline(10, 6, sheet, New CellRange(10, 2, 1, 4)))
            sheet.SparklineContainer.Add(New ExcelSparklineGroup(New ExcelSparklineSetting(), SparklineType.Winloss))
            sheet.SparklineContainer(2).Add(New ExcelSparkline(12, 6, sheet, New CellRange(12, 2, 1, 4)))
        End Sub

        Private Sub InitControls()
            AddHandler btnGroup.Click, AddressOf BtnGroup_Click
            AddHandler btnUngroup.Click, AddressOf BtnUngroup_Click
            sparklineSettingPanel1.SetSparklineSettingValues(New ExcelSparklineSetting())
            AddHandler sparklineSettingPanel1.SparklineSettingChanged, AddressOf SparklineSettingPanel1_SparklineSettingChanged
            AddHandler sparklineSettingPanel1.SparklineRendererChanged, AddressOf SparklineSettingPanel1_SparklineRendererChanged
        End Sub

        Private Function GetSparklineType(ByVal sparklineRenderer As ISparklineRenderer) As SparklineType
            If TypeOf sparklineRenderer Is ColumnSparklineRenderer Then Return SparklineType.Column
            If TypeOf sparklineRenderer Is WinLossSparklineRenderer Then Return SparklineType.Winloss
            Return SparklineType.Line
        End Function

        Private Function GetActiveSparklineGroup() As ISparklineGroup
            Dim range As CellRange = fpSpread.ActiveSheet.GetSelection(0)
            Dim sparkline As ISparkline = fpSpread.ActiveSheet.Cells(range.Row, range.Column).Sparkline?.Data
            Return FindSparklineGroup(fpSpread.ActiveSheet, sparkline)
        End Function

        Private Function FindSparklineGroup(ByVal sheet As SheetView, ByVal sparkline As ISparkline) As ISparklineGroup
            If sparkline Is Nothing Then Return Nothing
            Return sheet.SparklineContainer.Where(Function(group) group.Contains(sparkline)).FirstOrDefault()
        End Function

        Private Sub FpSpread_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
            Dim sparklineGroups As HashSet(Of ISparklineGroup) = New HashSet(Of ISparklineGroup)
            Dim range1 As FarPoint.Win.Spread.Model.CellRange
            For Each range1 In fpSpread.ActiveSheet.GetSelections()
                Dim sp As FarPoint.Win.Spread.Sparkline = fpSpread.ActiveSheet.Cells(range1.Row, range1.Column).Sparkline
                If sp IsNot Nothing Then
                    sparklineGroups.Add(FindSparklineGroup(fpSpread.ActiveSheet, sp.Data))
                End If
            Next

            btnGroup.Enabled = sparklineGroups.Count > 1
            If GetActiveSparklineGroup()?.Count > 1 Then
                btnUngroup.Enabled = True

            Else
                btnUngroup.Enabled = False
            End If

            sparklineSettingPanel1.Enabled = sparklineGroups.Count > 0
            Dim firstRange As CellRange = fpSpread.ActiveSheet.GetSelection(0)
            Dim sparkline As FarPoint.Win.Spread.Sparkline = fpSpread.ActiveSheet.GetSparkline(firstRange.Row, firstRange.Column)

            If sparkline IsNot Nothing Then
                Dim group = FindSparklineGroup(fpSpread.ActiveSheet, sparkline.Data)
                Dim setting = TryCast(group.Setting, ExcelSparklineSetting)
                sparklineSettingPanel1.SetSparklineSettingValues(setting, GetSparklineType(group.Renderer))
            End If
        End Sub

        Private Sub SparklineSettingPanel1_SparklineRendererChanged(ByVal sender As Object, ByVal e As SparklineRendererChangedEventArgs)
            GetActiveSparklineGroup().Renderer = e.Renderer
        End Sub

        Private Sub SparklineSettingPanel1_SparklineSettingChanged(ByVal sender As Object, ByVal e As SparklineSettingChangedEventArgs)
            GetActiveSparklineGroup().Setting = e.SparklineSetting
        End Sub

        Private Sub BtnUngroup_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim sparklines As List(Of FarPoint.Win.Spread.Sparkline) = New List(Of FarPoint.Win.Spread.Sparkline)()
            Dim range As CellRange
            For Each range In fpSpread.ActiveSheet.GetSelections()
                Dim sparkline As FarPoint.Win.Spread.Sparkline = fpSpread.ActiveSheet.Cells(range.Row, range.Column).Sparkline
                If sparkline IsNot Nothing Then sparklines.Add(sparkline)
            Next

            fpSpread.ActiveSheet.UngroupSparkline(sparklines.[Select](Function(sparkline) New CellRange(sparkline.Data.Row, sparkline.Data.Column, 1, 1)).ToArray())
            btnUngroup.Enabled = False
        End Sub

        Private Sub BtnGroup_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim sparklines As List(Of FarPoint.Win.Spread.Sparkline) = New List(Of FarPoint.Win.Spread.Sparkline)()
            Dim range As CellRange
            For Each range In fpSpread.ActiveSheet.GetSelections()
                Dim sparkline As FarPoint.Win.Spread.Sparkline = fpSpread.ActiveSheet.Cells(range.Row, range.Column).Sparkline
                If sparkline IsNot Nothing Then sparklines.Add(sparkline)
            Next

            Dim setting As ISparklineSetting = sparklines(0).Setting
            Dim sparklineGroup = fpSpread.ActiveSheet.GroupSparkline(sparklines.[Select](Function(sparkline) New CellRange(sparkline.Data.Row, sparkline.Data.Column, 1, 1)).ToArray())
            sparklineGroup.Setting = setting
            btnGroup.Enabled = False
            btnUngroup.Enabled = True
        End Sub
    End Class
End Namespace


'
'SparklineGroupsAndSettings.Designer.vb
'


Namespace Features
	Partial Class SparklineGroupsAndSettings
		''' <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 Overloads 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.btnUngroup = New System.Windows.Forms.Button()
			Me.btnGroup = New System.Windows.Forms.Button()
			Me.label1 = New System.Windows.Forms.Label()
			Me.sparklineSettingPanel1 = New Spread.Sparkline.SparklineSettingPanel()
			Me.pnlMain.SuspendLayout()
			Me.pnlControls.SuspendLayout()
			DirectCast(Me.fpSpread, System.ComponentModel.ISupportInitialize).BeginInit()
			DirectCast(Me.fpSpread_Sheet1, System.ComponentModel.ISupportInitialize).BeginInit()
			Me.groupBox1.SuspendLayout()
			Me.SuspendLayout()
			' 
			' pnlMain
			' 
			Me.pnlMain.Controls.Add(Me.fpSpread)
			Me.pnlMain.Size = New System.Drawing.Size(580, 606)
			' 
			' pnlControls
			' 
			Me.pnlControls.Controls.Add(Me.sparklineSettingPanel1)
			Me.pnlControls.Controls.Add(Me.groupBox1)
			Me.pnlControls.Size = New System.Drawing.Size(192, 606)
			' 
			' 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(580, 606)
			Me.fpSpread.Skin = FarPoint.Win.Spread.DefaultSpreadSkins.Office2016White
			Me.fpSpread.StatusBarVisible = True
			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.btnUngroup)
			Me.groupBox1.Controls.Add(Me.btnGroup)
			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(192, 123)
			Me.groupBox1.TabIndex = 0
			Me.groupBox1.TabStop = False
			Me.groupBox1.Text = "Sparkline Grouping"
			' 
			' btnUngroup
			' 
			Me.btnUngroup.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.btnUngroup.Enabled = False
			Me.btnUngroup.Location = New System.Drawing.Point(38, 89)
			Me.btnUngroup.Name = "btnUngroup"
			Me.btnUngroup.Size = New System.Drawing.Size(110, 23)
			Me.btnUngroup.TabIndex = 2
			Me.btnUngroup.Text = "Ungroup"
			Me.btnUngroup.UseVisualStyleBackColor = True
			' 
			' btnGroup
			' 
			Me.btnGroup.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.btnGroup.Enabled = False
			Me.btnGroup.Location = New System.Drawing.Point(38, 60)
			Me.btnGroup.Name = "btnGroup"
			Me.btnGroup.Size = New System.Drawing.Size(110, 23)
			Me.btnGroup.TabIndex = 1
			Me.btnGroup.Text = "Group"
			Me.btnGroup.UseVisualStyleBackColor = True
			' 
			' label1
			' 
			Me.label1.Dock = System.Windows.Forms.DockStyle.Top
			Me.label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CByte(0))
			Me.label1.Location = New System.Drawing.Point(3, 16)
			Me.label1.Name = "label1"
			Me.label1.Size = New System.Drawing.Size(186, 32)
			Me.label1.TabIndex = 0
			Me.label1.Text = "Select multiple Sparklines and Group/Ungroup selected sparklines"
			' 
			' sparklineSettingPanel1
			' 
			Me.sparklineSettingPanel1.Dock = System.Windows.Forms.DockStyle.Fill
			Me.sparklineSettingPanel1.Enabled = False
			Me.sparklineSettingPanel1.Location = New System.Drawing.Point(0, 123)
			Me.sparklineSettingPanel1.Name = "sparklineSettingPanel1"
			Me.sparklineSettingPanel1.Size = New System.Drawing.Size(192, 483)
			Me.sparklineSettingPanel1.TabIndex = 1
			' 
			' SparklineGroupsAndSettings
			' 
			Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
			Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
			Me.Name = "SparklineGroupsAndSettings"
			Me.Size = New System.Drawing.Size(773, 606)
			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.ResumeLayout(False)

		End Sub

		#End Region

		Private fpSpread As FarPoint.Win.Spread.FpSpread
		Private fpSpread_Sheet1 As FarPoint.Win.Spread.SheetView
        Private sparklineSettingPanel1 As Spread.Sparkline.SparklineSettingPanel
        Private groupBox1 As System.Windows.Forms.GroupBox
		Private btnUngroup As System.Windows.Forms.Button
		Private btnGroup As System.Windows.Forms.Button
		Private label1 As System.Windows.Forms.Label
	End Class
End Namespace
