Skip to main content Skip to footer

HowTo: Custom Sparkline in Spread for WinForms

Spread for Winforms provides a feature of creating sparklines wherein we can create a small graph in a cell that uses data from a range of cells. The data for the sparkline is limited to one column or row of values within the same SpreadSheet. We can set the SparklineType to column, line, or winloss etc. Here in this blog we will explain an approach to create custom winloss SparklineType from an external datasource wherein the data is fetched from some other sheet of FpSpread.

STEP 1 - Create Custom Class Inherited from ExcelSparkLine

First of all we need to create a CustomExcelSparkline class inheriting from ExcelSparkline class which takes SheetView as datasource as given below:-

class CustomExcelSparkline : ExcelSparkline  
{  
    CellRangeSegmentData innerData;  
    public CustomExcelSparkline(int row, int column, SheetView source)  
        : base(row, column, string.Empty)  
    {  
        innerData = new CellRangeSegmentData(source, 0, 0, source.RowCount, source.ColumnCount, null);  
    }  
    public override object GetSparkLineData()  
    {  
        return innerData;  
    }  
}

STEP 2 - Use Custom class to Create winloss SparklineType

Now we will use this CustomExcelSparkline to create winloss SparklineType which takes data from some other sheet of Spread on which it is drawn:-


private void Form1_Load(object sender, EventArgs e)  
{  
    fpSpread1.SuspendLayout();  
    List<Double> dlist = new List<double>();  
    dlist.Add(4.5);  
    dlist.Add(4.8);  
    dlist.Add(5.5);  
    dlist.Add(6.0);  
    dlist.Add(7.5);  

    SheetView sv = new SheetView();  
    sv.Visible = false;  
    sv.SheetName = Guid.NewGuid().ToString();  
    sv.DataSource = dlist;  
    fpSpread1.Sheets.Add(sv);  
    fpSpread1.Sheets[0].RowCount = 10;  
    fpSpread1.Sheets[0].ColumnCount = 10;  
    for (int r = 0; r < fpSpread1.Sheets[0].RowCount; r++)  
    {  
        FarPoint.Win.Spread.SparklineType type = SparklineType.Column;  
        if (r % 3 == 1)  
            type = SparklineType.Line;  
        else if (r % 3 == 2)  
            type = SparklineType.Winloss;  
        FarPoint.Win.Spread.ExcelSparklineGroup esg = new FarPoint.Win.Spread.ExcelSparklineGroup(new FarPoint.Win.Spread.ExcelSparklineSetting(), type);  
        for (int c = 0; c < fpSpread1.Sheets[0].ColumnCount; c++)  
        {  
            CustomExcelSparkline es = new CustomExcelSparkline(r, c, sv);  
            esg.Add(es);  
        }  
        fpSpread1.Sheets[0].SparklineContainer.Add(esg);  
    }  
    fpSpread1.ResumeLayout();  
}

winloss SpreadSparkline winloss SpreadSparkline Download sample for detailed implementation : DownloadSample_CS DownloadSample_VB

MESCIUS inc.

comments powered by Disqus