using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace iDiTect.Excel.Demo
{
    public static class SparklineHelper
    {
        public static void AddSparkline()
        {
            var dt = GetDataTable();

            var workbook = new Workbook();
            var worksheet = workbook.Worksheets.Add("Sparkline");

            //Bind data table to cells
            worksheet.Cell(2, 1).InsertData(dt);

            ///Add column type sparkline
            worksheet.Cell("N1").Value = "Column";
            var sparklineCol = worksheet.SparklineGroups.Add("N2:N7", "A2:M7");
            sparklineCol.Type = SparklineType.Column;
            sparklineCol.Style = SparklineTheme.Colorful1;
            //Show and highlight all data point
            sparklineCol.ShowMarkers = SparklineMarkers.All;


            ///Add line type sparkline
            worksheet.Cell("O1").Value = "Line";
            var sparklineLine = worksheet.SparklineGroups.Add("O2:O7", "A2:M7");
            sparklineLine.Type = SparklineType.Line;
            sparklineLine.Style = SparklineTheme.Accent1Darker25;
            //Only show and highlight the first/last/high/low point
            sparklineLine.ShowMarkers = SparklineMarkers.FirstPoint | SparklineMarkers.LastPoint | SparklineMarkers.HighPoint | SparklineMarkers.LowPoint;


            ///Add stacked type sparkline
            worksheet.Cell("P1").Value = "Stacked";
            var sparklineStacked = worksheet.SparklineGroups.Add("P2:P7", "A2:M7");
            sparklineStacked.Type = SparklineType.Stacked;

            workbook.Save("Sparkline.xlsx");
        }


        private static DataTable GetDataTable()
        {
            string[] products = { "Orange", "Apple", "Banana", "Pear", "Grape", "Watermelon" };
            DataTable dt = new DataTable("Fruit Sales");
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Jan", typeof(long));
            dt.Columns.Add("Feb", typeof(long));
            dt.Columns.Add("Mar", typeof(long));
            dt.Columns.Add("Apr", typeof(long));
            dt.Columns.Add("May", typeof(long));
            dt.Columns.Add("Jun", typeof(long));
            dt.Columns.Add("Jul", typeof(long));
            dt.Columns.Add("Aug", typeof(long));
            dt.Columns.Add("Sep", typeof(long));
            dt.Columns.Add("Oct", typeof(long));
            dt.Columns.Add("Nov", typeof(long));
            dt.Columns.Add("Dec", typeof(long));

            Random r = new Random();
            for (int row = 0; row < 6; row++)
            {
                var dr = dt.NewRow();
                dr[0] = products[row];

                for (int col = 1; col < 13; col++)
                {
                    dr[col] = r.Next(-100, 100);
                }

                dt.Rows.Add(dr);
            }

            return dt;
        }
    }
}
