ChartDesigner.RegisterCustomModelType(Type, Type) Method

Associates a model type with a chart element type.

Namespace: DevExpress.XtraCharts.Designer

Assembly: DevExpress.XtraCharts.v20.1.Wizard.dll

Declaration

public void RegisterCustomModelType(
    Type chartElementType,
    Type customModelType
)
Public Sub RegisterCustomModelType(
    chartElementType As Type,
    customModelType As Type
)

Parameters

Name Type Description
chartElementType Type

The chart element type.

customModelType Type

The model type.

Remarks

Use the RegisterCustomModelType method to register a model for a custom chart element.

Examples

This example illustrates how to create and register a model (the CustomPointColorizerModel class in this example) for a custom colorizer (the CustomPointColorizer class in this example).

NOTE

Visit the How to Create a Model for a Custom Chart Element GitHub page to review and download a complete sample project.

Step 1. Create a Model

For example, you have a custom colorizer CustomPointColorizer class that looks as follows:

[TypeConverter(typeof(ExpandableObjectConverter))]
public class CustomPointColorizer : ChartColorizerBase {
    double threshold = 60;
    Color lower = Color.Red;
    Color upper = Color.Green;

    public double Value {
        get { return threshold; }
        set { threshold = value; }
    }
    public Color LowerValuePointColor {
        get { return lower; }
        set { lower = value; }
    }
    public Color UpperValuePointColor {
        get { return upper; }
        set { upper = value; }
    }
    public override Color GetAggregatedPointColor(object argument, object[] values, SeriesPoint[] points, Palette palette) {
        if ((double)values[0] > Value)
            return UpperValuePointColor;
        else
            return LowerValuePointColor;
    }
    public override Color GetPointColor(object argument, object[] values, object colorKey, Palette palette) {
        return Color.Empty;
    }
    protected override ChartElement CreateObjectForClone() {
        return new CustomPointColorizer();
    }
    public override void Assign(ChartElement obj) {
        base.Assign(obj);
        CustomPointColorizer colorizer = obj as CustomPointColorizer;
        if (colorizer != null) {
            Value = colorizer.Value;
            LowerValuePointColor = colorizer.LowerValuePointColor;
            UpperValuePointColor = colorizer.UpperValuePointColor;
        }
    }
    public override string ToString() {
        return "(CustomPointColorizer)";
    }
}

Next, create a CustomPointColorizerModel model for your colorizer. This model is used to modify the colorizer options in the Chart Designer's Properties tab.

public class CustomPointColorizerModel : ChartColorizerBaseModel {
    CustomPointColorizer MyColorizer { get { return (CustomPointColorizer)Colorizer; } } 

    public double Value {
        get { return MyColorizer.Value; }
        set { SetProperty("Value", value); }
    }
    public Color LowerValuePointColor {
        get { return MyColorizer.LowerValuePointColor; }
        set { SetProperty("LowerValuePointColor", value); }
    }
    public Color UpperValuePointColor {
        get { return MyColorizer.UpperValuePointColor; }
        set { SetProperty("UpperValuePointColor", value); }
    }
    public CustomPointColorizerModel(ChartColorizerBase element, CustomModelProvider customModelProvider) : base(element, customModelProvider) {
    }
}

Step 2. Customize the Property Editor

Use the Editor attribute to configure an editor for a Chart Designer property. In this example, you add the CustomPointColorizer item to the list of colorizers for the bar series model's Colorizer property.

public class SideBySideBarSeriesViewCustomModel : SideBySideBarSeriesViewModel {
    [Editor(typeof(CustomColorizerEditor), typeof(UITypeEditor))]
    public new ChartColorizerBaseModel Colorizer {
        get { return base.Colorizer; }
        set { base.Colorizer = value; }
    }

    public SideBySideBarSeriesViewCustomModel(SideBySideBarSeriesView element, CustomModelProvider customModelProvider)
        : base(element, customModelProvider) {
    }
}

public class CustomColorizerEditor : UITypeEditor {
    IWindowsFormsEditorService editorService;
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
        editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
        var seriesView = context.Instance as SeriesViewBaseModel;

        List<ChartColorizerBase> colorizers = GetColorizers();

        CustomModelProvider modelProvider = new CustomModelProvider();
        modelProvider.RegisterCustomModelType(typeof(CustomPointColorizer), typeof(CustomPointColorizerModel));

        var listBox = new ListBoxControl();
        listBox.Click += listBox_Click;
        listBox.Items.Add("(None)");
        foreach (ChartColorizerBase colorizer in colorizers) {
            var colorizerModel = ModelHelper.GetModel<ChartColorizerBaseModel>(colorizer, modelProvider);
            int index = listBox.Items.Add(colorizerModel);
            if (value != null && colorizerModel.GetType() == value.GetType()) {
                listBox.SelectedIndex = index;
            }
        }
        editorService.DropDownControl(listBox);
        if (listBox.SelectedIndex != 0)
            if (value == null || listBox.SelectedItem.GetType() != value.GetType())
                return listBox.SelectedItem;
            else
                return value;
        else
            return null;
    }
    void listBox_Click(object sender, EventArgs e) {
        this.editorService.CloseDropDown();
    }
    List<ChartColorizerBase> GetColorizers() {
        return new List<ChartColorizerBase>() { new CustomPointColorizer(), new KeyColorColorizer(), new RangeColorizer() };
    }
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
        return UITypeEditorEditStyle.DropDown;
    }
}

Step 3. Register Models

To associate the model with the chart element, use the ChartDesigner.RegisterCustomModelType(System.Type,System.Type) method.

void OnButtonClick(object sender, EventArgs e) {
    ChartDesigner chartDesigner = new ChartDesigner((ChartControl)this.Controls["MyChart"]);
    chartDesigner.RegisterCustomModelType(typeof(CustomPointColorizer), typeof(CustomPointColorizerModel));
    chartDesigner.RegisterCustomModelType(typeof(SideBySideBarSeriesView), typeof(SideBySideBarSeriesViewCustomModel));
    chartDesigner.ShowDialog(true);
}
See Also