English

How To: Make chart colors match the colors in the view's legend

Summary

There is no dynamic link between a chart and a view. Use this script when you need to display a chart with the same colors used in a view's legend.

Procedure

The following sample script will change the colors in a chart to match the colors used in the view's legend. The legend must use the Unique Value legend type.

  1. Open a new script window.

    A. Activate the Project window.
    B. Click the Scripts icon.
    C. Click New.

  2. Copy the following code into the script window.

    Code:
    theView = av.GetActiveDoc
    theTheme = theView.GetActiveThemes.Get(0)

    theLegend = theTheme.GetLegend
    theType = theLegend.GetLegendType
    If (theType <> #LEGEND_TYPE_UNIQUE) then
    MsgBox.Info("This script will only work when using the"+nl+
    "'Unique Value' legend type.","Wrong Legend Type")
    return nil
    End

    theDocs = av.GetProject.GetDocs
    theChartList = {}

    for each d in theDocs
    If (d.GetClass.GetClassName = "Chart") then
    theChartList.Add(d)
    end
    end
    theChart = MsgBox.ListAsString(theChartList,
    "Select the appropriate Chart","Chart Select")

    if(theChart = Nil) then
    return nil
    elseIf (theChart.IsSeriesFromRecords.Not) then
    return nil
    end

    theChartDisplay = theChart.GetChartDisplay

    for each i in 0..(theChartDisplay.GetNumSeries - 1)
    seriesname = theChart.GetSeriesLabel(i)
    'msgbox.info(seriesname,"SeriesName")
    for each x in 0..(theLegend.GetNumClasses - 1)
    classname = theLegend.ReturnClassInfo(x).Get(0) ' label name
    'msgbox.info(classname,"ClassName")
    if(seriesname = classname) then
    aSymbol = theLegend.ReturnClassInfo(x).get(2) ' symbol
    aColor = aSymbol.GetColor
    theChartDisplay.SetSeriesColor(i,aColor)
    break
    end
    end
    end

    If (seriesname <> classname) then
    MsgBox.Info("The specified Chart does not have a field selected in the"+nl+
    "'Label serieds using:' box in the Chart Properties Dialog."+nl+
    "Please select the same field used to classify the Legend"+nl+
    "in the View.","")
    end

  3. Click the Compile button.
    [O-Image] Script compile button
  4. Make the appropriate view and theme active.
  5. Click on the title bar of the script window.
  6. Click the Run button.
    [O-Image] Run compiled script button
    Note:
    This script will only change the color of the chart symbols. Only a solid fill pattern can be used to symbolize a chart.