Change color for a certain member on charts

Flexmonster Pivot Charts

by Flexmonster Pivot Table

HTML

<script src="https://cdn.flexmonster.com/flexmonster.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinycolor/1.4.1/tinycolor.min.js"></script>

<div>
  <h2>How to change color for a certain member on charts</h2>
  <p>
    This example demonstrates how you can customize a chart sector based on its
    data.
  </p>
  <p>
    To customize the pivot charts, use the
    <a
      href="https://www.flexmonster.com/api/customizechartelement/"
      target="_blank"
      >customizeChartElement()</a
    >
    method. In this example, we check whether chart's data contains the
    <code>United States</code> member. If it does, we change the chart sector's
    color.
  </p>
  <p>
    <a
      href="https://www.flexmonster.com/doc/customizing-pivot-charts/"
      target="_blank"
    >Visit our docs to learn more about customizing the charts</a>.
  </p>
</div>

<div id="pivot-container"></div>

CSS

@import url(https://cdn.flexmonster.com/assets/jsfiddle-styles.css);

.fm-charts-color-1 {
  fill: rgb(0, 164, 90) !important;
}

.fm-charts-color-2 {
  fill: rgb(223, 56, 0) !important;
}

.fm-charts-color-3 {
  fill: rgb(255, 184, 0) !important;
}

.fm-charts-color-4 {
  fill: rgb(109, 59, 216) !important;
}

.fm-charts-color-5 {
  fill: rgb(0, 117, 255) !important;
}

.fm-charts-color-6 {
  fill: rgb(206, 151, 230) !important;
}

.fm-charts-color-7 {
  fill: none !important;
}

#fm-pivot-view .fm-bar,
#fm-pivot-view .fm-charts-view .fm-chart-legend ul li .fm-icon-display,
#fm-pivot-view .fm-line,
#fm-pivot-view .fm-arc path,
#fm-pivot-view .fm-bar-stack,
#fm-pivot-view .fm-scatter-point {
  opacity: 70% !important;
}

JavaScript

const pivot = new Flexmonster({
  container: "pivot-container",
  componentFolder: "https://cdn.flexmonster.com/",
  height: 600,
  toolbar: true,
  customizeChartElement: customizeChartElementFunction,
  report: {
    dataSource: {
      filename: "data/data.csv",
    },
    slice: {
      rows: [
        {
          uniqueName: "Business Type",
        },
      ],
      columns: [
        {
          uniqueName: "Country",
          filter: {
            exclude: ["country.[australia]", "country.[canada]"],
          },
        },
        {
          uniqueName: "Category",
          filter: {
            exclude: ["category.[clothing]", "category.[cars]"],
          },
        },
        {
          uniqueName: "[Measures]",
        },
      ],
      measures: [
        {
          uniqueName: "Price",
          format: "currency",
        },
        {
          uniqueName: "Quantity",
          aggregation: "sum",
        },
      ],
    },
    options: {
      viewType: "charts",
      chart: {
        type: "column",
        pieDataIndex: "3",
      },
    },
    formats: [
      {
        name: "currency",
        currencySymbol: "$",
      },
    ],
  },
});

function customizeChartElementFunction(element, data) {
  let newColor = tinycolor("#1a0019");
  let memberToHighlight = "country.[united states]";

  if (contains(data, memberToHighlight)) {
    switch (data.chartType) {
      case "line":
      case "scatter":
        element.style.stroke = applyShade(
          newColor,
          element.style.stroke,
        );
        break;
      case "pie":
        if (element.querySelector("path")) {
          element.querySelector("path").style.fill = applyShade(
            newColor,
            element.querySelector("path").style.fill,
          );
        }
        break;
      case "column_line":
        if (element.classList.contains("fm-line") ||...