Chart for .NET / User's Guide / Styling / Style Sheet Configurators

In This Topic
    Style Sheet Configurators
    In This Topic

    In many cases you may want to create a style sheet that is similar to the predefined one, but with different stroke or filling. For example when you don’t like the color of the label or want to apply a gradient to the chart walls. This is where style sheet configurators can come handy. In a nut shell they allow you to quickly create a style sheet based on a chart palette.

     Chart Palette

    Predefined style sheets as well as style sheet configurators use an object called NChartPalette that associates colors to chart elements. When creating a chart palette you can choose from one of the 9 predefined palettes or create your own. The following code snippet loads the predefined “Nevron” palette:

    C#
    Copy Code
    NChartPalette palette = new NChartPalette(ChartPredefinedPalette.Nevron);
    palette.LabelForeColor = Color.DarkGray;
    
    Visual Basic
    Copy Code
    Dim palette As NChartPalette = New NChartPalette()
    palette.LabelForeColor = Color.DarkGray 
    

    Now you have a palette that differs slightly from the standard “Nevron” palette. To make some use of it you should create a style sheet based on the color entries in the palette. The following code does that: 

    C#
    Copy Code
    // create palette
    NChartPalette palette = new NChartPalette(ChartPredefinedPalette.Nevron);
    palette.LabelForeColor = Color.DarkGray;
    // build a style sheet based on the palette
    NFillStyleSheetConfigurator fillConfigurator = new NFillStyleSheetConfigurator();
    fillConfigurator.Palette = palette;
    fillConfigurator.WallFillTemplate = new NGradientFillStyleTemplate(GradientStyle.Horizontal, GradientVariant.Variant1);
    NStyleSheet sheet = new NStyleSheet();
    fillConfigurator.ConfigureSheet(sheet);
    // apply style sheet to document
    sheet.Apply(nChartControl1.Document);
    
    Visual Basic
    Copy Code
    ' create palette
    Dim palette As NChartPalette = New NChartPalette(ChartPredefinedPalette.Nevron)
    palette.LabelForeColor = Color.DarkGray
    ' build a style sheet based on the palette
    Dim fillConfigurator As NFillStyleSheetConfigurator = New NFillStyleSheetConfigurator()
    fillConfigurator.Palette = palette
    fillConfigurator.WallFillTemplate = New NGradientFillStyleTemplate(GradientStyle.Horizontal, GradientVariant.Variant1)
    Dim sheet As NStyleSheet = New NStyleSheet()
    fillConfigurator.ConfigureSheet(sheet)
    ' apply style sheet to document
    sheet.Apply(NChartControl1.Document)
    

    Now let’s take a closer look at the above code. First we create a palette based on a set of predefined colors – in this case the standard “Nevron” palette, with altered label fore color. Afterwards the code creates a fill style configurator. The configurator uses a palette and a set of fill style templates to create style sheet rules. The above code changes the fill template for walls to gradient, horizontal fill. Finally the fill configurator is used to configure the style sheet and the style sheet is applied on the chart document. The following table lists the colors you can control from the chart palette:

    Color Entry Description
    ControlBackgroundForeColor Controls the foreground color of the control background.
    ControlBackgroundBackColor Controls the background color of the control background.
    GaugeBackgroundBackColor Controls the foreground color of gauges background.
    GaugeBackgroundForeColor Controls the background color of gauges background.
    LabelForeColor Controls the foreground color of labels.
    LabelBackColor Controls the background color of labels.
    WallForeColor Controls the foreground color of chart walls.
    WallBackColor

    Controls the background color of chart walls.

    IndicatorForeColor Controls the foreground color of gauge indicators.
    IndicatorBackColor Controls the background color of gauge indicators.
    RulerStrokeColor Controls the color of the stroke style applied to axis rulers.
    TickStrokeColor Control the color of the stroke style applied to ticks.
    GridStrokeColor Controls the color of the stroke style applied to axis grid lines.
    SeriesColors Controls the color of series data items, or series within a chart.

     Style Sheet Configurators

    Currently there are two types of style sheet configurators – fill style configurators and stroke style configurators. The code in the previous section used the fill style configurator to create a sample style sheet based on the “Nevron” color palette. However in many cases you may also want to alter the stroke style applied on chart elements as well. The following code snippet shows how to stack the output of a fill and stroke style configurators and then apply the result style sheet to the chart document: 

    C#
    Copy Code
    // create a fill style sheet configurator
    NFillStyleSheetConfigurator fillStyleSheet = new NFillStyleSheetConfigurator();
    fillStyleSheet.Palette.SetPredefinedPalette(ChartPredefinedPalette.Nature);
    fillStyleSheet.MultiColorSeries = true;
    // create a stroke style sheet configuratorNStrokeStyleSheetConfigurator strokeStyleSheet = new NStrokeStyleSheetConfigurator();
    strokeStyleSheet.Palette.SetPredefinedPalette(ChartPredefinedPalette.Nature);
    strokeStyleSheet.ApplyToDataLabels = true;
    strokeStyleSheet.MultiColorSeries = true;
    // create a style sheet
    NStyleSheet sheet = new NStyleSheet();
    // configure the style sheet
    fillStyleSheet.ConfigureSheet(sheet);
    strokeStyleSheet.ConfigureSheet(sheet);
    // apply to document
    sheet.Apply(nChartControl1.Document);
    
    Visual Basic
    Copy Code
    ' create a fill style sheet configurator
    Dim fillStyleSheet As NFillStyleSheetConfigurator = New NFillStyleSheetConfigurator()
    fillStyleSheet.Palette.SetPredefinedPalette(ChartPredefinedPalette.Nature)
    fillStyleSheet.MultiColorSeries = True
    ' create a stroke style sheet configurator
    Dim strokeStyleSheet As NStrokeStyleSheetConfigurator = New NStrokeStyleSheetConfigurator()
    strokeStyleSheet.Palette.SetPredefinedPalette(ChartPredefinedPalette.Nature)
    strokeStyleSheet.ApplyToDataLabels = True
    strokeStyleSheet.MultiColorSeries = True
    ' create a style sheet
    Dim sheet As NStyleSheet = New NStyleSheet()
    ' configure the style sheet
    fillStyleSheet.ConfigureSheet(sheet)
    strokeStyleSheet.ConfigureSheet(sheet)
    ' apply to document
    sheet.Apply(NChartControl1.Document)
    
     Related Examples

    Windows Forms: Styles\Style Sheets\Predefined Style Sheets

    Windows Forms: Styles\Style Sheets\Style Sheet Configurators

    Web Forms: Styles\Style Sheets\Predefined Style Sheets

    Web Forms: Styles\Style Sheets\Style Sheet Configurators