Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <link href="https://cdn.webdatarocks.com/latest/webdatarocks.min.css" rel="stylesheet"/>
<script src="https://cdn.webdatarocks.com/latest/webdatarocks.toolbar.min.js"></script>
<script src="https://cdn.webdatarocks.com/latest/webdatarocks.js"></script>
<script src="https://cdn.webdatarocks.com/latest/webdatarocks.amcharts.js"></script>
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>

  <h1>Profit by Country</h1>
<table>
  <tr>
    <td><div id="pivotContainer-1"></div></td>
    <td><div id="columnChartContainer"></div></td>
  </tr>
  <tr>
    <td><div id="barChartContainer"></div></td>
    <td><div id="pivotContainer-2"></div></td>
  </tr>
</table>
              
            
!

CSS

              
                body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

h1 {
  text-align: center;
}

#columnChartContainer, #barChartContainer {
  width: 100%;
  height: 500px;
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0 10px;
}

td {
  width: 50%;
}

              
            
!

JS

              
                var pivot = new WebDataRocks({
    container: "#pivotContainer-1",
    toolbar: true,
    height: 580,
    width: "100%",
    report: {
        dataSource: {
            data: getData()
        },
        "slice": {
        "rows": [
            {
                "uniqueName": "Country"
            }
        ],
        "columns": [
            {
                "uniqueName": "Product Category"
            },
            {
                "uniqueName": "Measures"
            }
        ],
        "measures": [
            {
                "uniqueName": "Profit",
                "aggregation": "sum",
                "format": "currency"
            }
        ],
        "expands": {
            "rows": [
                {
                    "tuple": [
                        "Country.France"
                    ]
                },
                {
                    "tuple": [
                        "Country.Spain"
                    ]
                }
            ]
        }
    },
        "conditions": [{
                "formula": "#value > 15000",
                "measure": "Profit",
                "format": {
                    "backgroundColor": "#0598df",
                    "color": "#FFFFFF",
                    "fontFamily": "Arial",
                    "fontSize": "12px"
                }
            }
        ],
        "formats": [{
            "name": "currency",
            "thousandsSeparator": ",",
            "decimalSeparator": ".",
            "decimalPlaces": 2,
            "currencySymbol": "$",
            "currencySymbolAlign": "left",
            "nullValue": "",
            "textAlign": "right",
            "isPercent": false
        }]
    },
    reportcomplete: function() {
        createColumnAmChart();
    }
});

var chart;

function createColumnAmChart() {
    /* Get all the data from the pivot grid (alternatively, a slice can be passed to getData() as the first argument) */
    pivot.amcharts.getData({

    }, drawChart, updateChart);
}

function drawChart(chartData, rawData) {

    /* Apply amCharts theme */
    am4core.useTheme(am4themes_animated);
    /* Create chart instance */
    chart = am4core.create("columnChartContainer", am4charts.XYChart);
    chart.hiddenState.properties.opacity = 0;

    /* Add data processed by WebDataRocks to the chart */
    chart.data = chartData.data;
    /* Create axes */
    /* Category axis */
    var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
    categoryAxis.renderer.grid.template.location = 0;
    categoryAxis.dataFields.category = pivot.amcharts.getCategoryName(rawData);
    categoryAxis.renderer.minGridDistance = 40;
    categoryAxis.fontSize = 11;
    categoryAxis.renderer.labels.template.dy = 5;
    /* Value axis */
    var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
    valueAxis.numberFormatter = new am4core.NumberFormatter();
    /* Get number formatting from WebDataRocks prepared to a format required by amCharts */
    var numberFormat = pivot.amcharts.getNumberFormatPattern(rawData.meta.formats[0]);
    /* Apply number formatting to the chart */
    valueAxis.numberFormatter.numberFormat = numberFormat;

    valueAxis.min = 0;
    valueAxis.renderer.minGridDistance = 30;
    valueAxis.renderer.baseGrid.disabled = true;
    /* Create and configure series */
    var series = chart.series.push(new am4charts.ColumnSeries());
    series.dataFields.categoryX = pivot.amcharts.getCategoryName(rawData);
    series.dataFields.valueY = pivot.amcharts.getMeasureNameByIndex(rawData, 0);
    series.columns.template.tooltipText = "{valueY.value.formatNumber('" + numberFormat + "')}";
    series.columns.template.tooltipY = 0;
    series.columns.template.strokeOpacity = 0;

    // as by default columns of the same series are of the same color, we add adapter which takes colors from chart.colors color set
    series.columns.template.adapter.add("fill", function(fill, target) {
        return chart.colors.getIndex(target.dataItem.index);
    });


    /* Add flags icons to the columns */

    var image = new am4core.Image();
    image.horizontalCenter = "middle";
    image.width = 20;
    image.height = 20;
    image.verticalCenter = "middle";
    image.adapter.add("href", (href, target) => {
        let category = target.dataItem.category;
        if (category) {
            return "https://www.amcharts.com/wp-content/uploads/flags/" + category.split(" ").join("-").toLowerCase() + ".svg";
        }
        return href;
    })
    categoryAxis.dataItems.template.bullet = image;
}

function updateChart(chartData, rawData) {
    chart.dispose();
    drawChart(chartData, rawData);
}

var pivot2 = new WebDataRocks({
    container: "#pivotContainer-2",
    toolbar: true,
    height: 580,
    width: "100%",
    report: {
    "dataSource": {
        "dataSourceType": "csv",
        "filename": "https://cdn.webdatarocks.com/data/data.csv"
    },
    "slice": {
        "rows": [
            {
                "uniqueName": "Country"
            }
        ],
        "columns": [
            {
                "uniqueName": "Category"
            },
            {
                "uniqueName": "Measures"
            }
        ],
        "measures": [
            {
                "uniqueName": "Price",
                "aggregation": "average",
                "format": "3ys5qejd"
            },
            {
                "uniqueName": "Median of Price",
                "formula": "median(\"Price\") ",
                "caption": "Median of Price"
            },
        ]
    },
    "options": {
        "grid": {
            "type": "classic"
        }
    },
    "conditions": [
        {
            "formula": "#value < 600",
            "measure": "Price",
            "format": {
                "backgroundColor": "#f45328",
                "color": "#FFFFFF",
                "fontFamily": "Arial",
                "fontSize": "12px"
            }
        },
        {
            "formula": "#value > 30000",
            "measure": "Price",
            "format": {
                "backgroundColor": "#0598df",
                "color": "#FFFFFF",
                "fontFamily": "Arial",
                "fontSize": "12px"
            }
        }
    ],
    "formats": [
        {
            "name": "3ys5qejd",
            "thousandsSeparator": " ",
            "decimalSeparator": ".",
            "decimalPlaces": 2,
            "currencySymbol": "$",
            "currencySymbolAlign": "right",
            "nullValue": "",
            "textAlign": "right",
            "isPercent": false
        },
        {
            "name": "",
            "thousandsSeparator": " ",
            "decimalSeparator": ".",
            "decimalPlaces": 2,
            "currencySymbol": "",
            "currencySymbolAlign": "left",
            "nullValue": "",
            "textAlign": "right",
            "isPercent": false
        }
    ]
},
    reportcomplete: function() {
        createBarChart();
    }
});

var barChart;

function createBarChart() {
    /* Get all the data from the pivot grid (alternatively, a slice can be passed to getData() inside the options object - the first argument) */
    pivot2.amcharts.getData({}, drawBarChart, updateBarChart);
}

function drawBarChart(chartData, rawData) {
    /* Apply amCharts theme */
    am4core.useTheme(am4themes_animated);

    /* Create chart instance */
    barChart = am4core.create("barChartContainer", am4charts.XYChart);

    /* Add data processed by WebDataRocks to the chart */
    barChart.data = chartData.data;

    barChart.legend = new am4charts.Legend();
    barChart.legend.position = "right";
    barChart.fontSize=10;

    /* Create and configure axes */
    var categoryAxis = barChart.yAxes.push(new am4charts.CategoryAxis());
    categoryAxis.dataFields.category = pivot2.amcharts.getCategoryName(rawData);
    categoryAxis.renderer.grid.template.opacity = 0;

    var valueAxis = barChart.xAxes.push(new am4charts.ValueAxis());
    valueAxis.min = 0;
    valueAxis.renderer.grid.template.opacity = 0;
    valueAxis.renderer.ticks.template.strokeOpacity = 0.5;
    valueAxis.renderer.ticks.template.stroke = am4core.color("#495C43");
    valueAxis.renderer.ticks.template.length = 10;
    valueAxis.renderer.line.strokeOpacity = 0.5;
    valueAxis.renderer.baseGrid.disabled = true;
    valueAxis.renderer.minGridDistance = 100;

    /* Get formatting from WebDataRocks */
    var numberFormat = pivot2.amcharts.getNumberFormatPattern(rawData.meta.formats[0]);

    /* Apply number formatting to the chart */
    categoryAxis.numberFormatter.numberFormat = numberFormat;

    /* Create and configure series for a stacked bar chart */
    for (s = 0; s < pivot2.amcharts.getNumberOfMeasures(rawData); s++) {

        var series = barChart.series.push(new am4charts.ColumnSeries());
        series.dataFields.valueX = pivot2.amcharts.getMeasureNameByIndex(rawData, s);
        series.dataFields.categoryY = pivot2.amcharts.getCategoryName(rawData);
        series.stacked = true;
        series.name = pivot2.amcharts.getMeasureNameByIndex(rawData, s);

        var labelBullet = series.bullets.push(new am4charts.LabelBullet());
        labelBullet.locationX = 0.5;
        labelBullet.label.text = "{valueX}";
        labelBullet.label.fill = am4core.color("#fff");
    }
}

function updateBarChart(chartData, rawData) {
    barChart.dispose();
    drawBarChart(chartData, rawData);
}

function getData() {
    return [{
            "Country": {
                type: "string"
            },
            "Product Category": {
                type: "string"
            },
            "Profit": {
                type: "number"
            },
            "Actual Expenses": {
                type: "number"
            },
            "Budgeted Expenses": {
                type: "number"
            },
            "Date": {
                type: "date string"
            },
            "Price Per Unit": {
                type: "number"
            },
        }, {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 455,
            "Actual Expenses": 250,
            "Budgeted Expenses": 55,
            "Date": "2015-02-14T07:34:08",
            "Price Per Unit": 45
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 156,
            "Actual Expenses": 501,
            "Budgeted Expenses": 55,
            "Date": "2015-02-14T07:34:08",
            "Price Per Unit": 48
        },
        {
            "Country": "Spain",
            "Product Category": "Entertainment equipment",
            "Profit": 455,
            "Actual Expenses": 302,
            "Budgeted Expenses": 75,
            "Date": "2016-01-11T07:28:30",
            "Price Per Unit": 95
        },
        {
            "Country": "Spain",
            "Product Category": "Entertainment equipment",
            "Profit": 455,
            "Actual Expenses": 205,
            "Budgeted Expenses": 75,
            "Date": "2016-01-11T07:28:30",
            "Price Per Unit": 14
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 236,
            "Actual Expenses": 63,
            "Budgeted Expenses": 55,
            "Date": "2017-11-27T06:52:07",
            "Price Per Unit": 45
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 355,
            "Actual Expenses": 140,
            "Budgeted Expenses": 55,
            "Date": "2017-11-27T06:52:07",
            "Price Per Unit": 43
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 354,
            "Actual Expenses": 88,
            "Budgeted Expenses": 65,
            "Date": "2017-10-13T05:34:44",
            "Price Per Unit": 45
        }, {
            "Country": "United States",
            "Product Category": "Sports equipment",
            "Profit": 354,
            "Actual Expenses": 170,
            "Budgeted Expenses": 65,
            "Date": "2017-10-13T05:34:44",
            "Price Per Unit": 45
        },
        {
            "Country": "France",
            "Product Category": "Sports equipment",
            "Profit": 354,
            "Actual Expenses": 230,
            "Budgeted Expenses": 55,
            "Date": "2014-11-20T07:16:26",
            "Price Per Unit": 45
        },
        {
            "Country": "France",
            "Product Category": "Sports equipment",
            "Profit": 354,
            "Actual Expenses": 160,
            "Budgeted Expenses": 25,
            "Date": "2018-12-18T01:26:57",
            "Price Per Unit": 22
        },
        {
            "Country": "France",
            "Product Category": "Sports equipment",
            "Profit": 352,

            "Actual Expenses": 180,

            "Date": "2015-09-12T05:29:36",
            "Price Per Unit": 89
        },
        {
            "Country": "France",

            "Product Category": "Entertainment equipment",
            "Profit": 654,

            "Actual Expenses": 190,
            "Budgeted Expenses": 23,

            "Date": "2016-06-13T11:43:37",
            "Price Per Unit": 78
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 355,
            "Actual Expenses": 140,
            "Budgeted Expenses": 55,
            "Date": "2015-10-03T05:41:44",
            "Price Per Unit": 23
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 770,
            "Actual Expenses": 177,
            "Budgeted Expenses": 23,
            "Date": "2014-04-28T06:05:53",
            "Price Per Unit": 15
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 770,
            "Actual Expenses": 200,
            "Budgeted Expenses": 45,
            "Date": "2014-06-13T03:03:22",
            "Price Per Unit": 44
        },
        {

            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 770,

            "Actual Expenses": 300,
            "Budgeted Expenses": 55,

            "Date": "2015-07-28T12:04:26",
            "Price Per Unit": 22
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 770,
            "Actual Expenses": 140,
            "Budgeted Expenses": 55,
            "Date": "2014-12-31T10:21:58",
            "Price Per Unit": 45
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 550,

            "Actual Expenses": 120,
            "Budgeted Expenses": 55,

            "Date": "2017-09-09T07:11:20",
            "Price Per Unit": 88
        },
        {

            "Country": "France",
            "Product Category": "Entertainment equipment",
            "Profit": 655,
            "Actual Expenses": 88,
            "Budgeted Expenses": 45,
            "Date": "2014-06-15T12:41:23",
            "Price Per Unit": 35
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 354,
            "Actual Expenses": 90,
            "Budgeted Expenses": 55,
            "Date": "2017-12-08T11:25:50",
            "Price Per Unit": 74
        },
        {
            "Country": "France",
            "Product Category": "Cameras",
            "Profit": 322,
            "Actual Expenses": 30,
            "Budgeted Expenses": 55,
            "Date": "2018-03-18T04:39:25",
            "Price Per Unit": 41
        },
        {
            "Country": "France",
            "Product Category": "Cameras",
            "Profit": 322,
            "Actual Expenses": 140,
            "Budgeted Expenses": 55,
            "Date": "2014-11-18T11:59:17",
            "Price Per Unit": 44
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 774,
            "Actual Expenses": 220,
            "Budgeted Expenses": 123,
            "Date": "2016-08-06T03:38:09",
            "Price Per Unit": 99
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 436,
            "Actual Expenses": 130,
            "Budgeted Expenses": 123,
            "Date": "2014-07-16T08:27:06",
            "Price Per Unit": 36
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 655,
            "Actual Expenses": 70,
            "Budgeted Expenses": 123,
            "Date": "2019-02-01T01:16:28",
            "Price Per Unit": 98
        },
        {
            "Country": "United States",
            "Product Category": "Entertainment equipment",
            "Profit": 455,
            "Actual Expenses": 140,
            "Budgeted Expenses": 123,
            "Date": "2019-02-01T01:16:28",
            "Price Per Unit": 105
        },

        {
            "Country": "Australia",
            "Product Category": "Entertainment equipment",
            "Profit": 1500,
            "Actual Expenses": 140,
            "Budgeted Expenses": 123,
            "Date": "2019-02-01T01:16:28",
            "Price Per Unit": 105
        },

        {
            "Country": "Italy",
            "Product Category": "Entertainment equipment",
            "Profit": 5000,
            "Actual Expenses": 140,
            "Budgeted Expenses": 123,
            "Date": "2019-02-01T01:16:28",
            "Price Per Unit": 105
        },

        {
            "Country": "Sweden",
            "Product Category": "Entertainment equipment",
            "Profit": 3405,
            "Actual Expenses": 140,
            "Budgeted Expenses": 123,
            "Date": "2019-02-01T01:16:28",
            "Price Per Unit": 105
        }
    ]
}
              
            
!
999px

Console