rramona2
5/20/2017 - 8:39 AM

Create a column clustered chart - Shared with Script Lab

Create a column clustered chart - Shared with Script Lab

name: Create a column clustered chart
description: Create a column clustered chart
author: rramona2
host: EXCEL
api_set: {}
script:
    content: |+
        $("#setup").click(setup);
        $("#create-column-clustered-chart").click(createChart);

        async function createChart() {
            try {
                await Excel.run(async (context) => {

                    const sheet = context.workbook.worksheets.getItem("Sample");
                
                    const salesTable = sheet.tables.getItem("SalesTable");

                    const dataRange = salesTable.getDataBodyRange();

                    let chart = sheet.charts.add("ColumnClustered", dataRange, "auto");

                    chart.setPosition("A15", "F30");
                    chart.title.text = "Quarterly sales chart";
                    chart.legend.position = "right"
                    chart.legend.format.fill.setSolidColor("white");
                    chart.dataLabels.format.font.size = 15;
                    chart.dataLabels.format.font.color = "black";
                    let points = chart.series.getItemAt(0).points;
                    points.getItemAt(0).format.fill.setSolidColor("pink");
                    points.getItemAt(1).format.fill.setSolidColor("indigo");

                    await context.sync();
                });
            }
            catch (error) {
                OfficeHelpers.UI.notify(error);
                OfficeHelpers.Utilities.log(error);
            }
        }

        async function setup() {
            try {
                await Excel.run(async (context) => {

                    await OfficeHelpers.ExcelUtilities.forceCreateSheet(context.workbook, "Sample");

                    const sheet = context.workbook.worksheets.getItem("Sample");

                    let expensesTable = sheet.tables.add('A1:E1', true);
                 
                    expensesTable.name = "SalesTable";

                    expensesTable.getHeaderRowRange().values = [["Product", "Qtr1", "Qtr2", "Qtr3", "Qtr4"]];

                    expensesTable.rows.add(null, [
                                  ["Frames", 5000, 7000, 6544, 4377],
                                  ["Saddles", 400, 323, 276, 651],
                                  ["Brake levers", 12000, 8766, 8456, 9812],
                                  ["Chains", 1550, 1088, 692, 853],
                                  ["Mirrors", 225, 600, 923, 544],
                                  ["Spokes", 6005, 7634, 4589, 8765]
                    ]);  

                    if (Office.context.requirements.isSetSupported("ExcelApi", 1.2)) {
                        sheet.getUsedRange().format.autofitColumns();
                        sheet.getUsedRange().format.autofitRows();
                    }

                    sheet.activate();

                    await context.sync();
                });
            }
            catch (error) {
                OfficeHelpers.UI.notify(error);
                OfficeHelpers.Utilities.log(error);
            }
        }


    language: typescript
template:
    content: |-
        <section class="ms-font-m">
            <p>This sample shows how to create a column clustered chart using the Excel JavaScript API.</p>
        </section>

        <section class="setup ms-font-m">
            <h3>Set up</h3>
            <button id="setup" class="ms-Button">
                <span class="ms-Button-label">Create table</span>
            </button>
        </section>

        <section class="samples ms-font-m">
            <h3>Try it out</h3>
            <button id="create-column-clustered-chart" class="ms-Button">
                <span class="ms-Button-label">Create a column clustered chart</span>
            </button>
        </section>
    language: html
style:
    content: "section.samples {\r\n    margin-top: 20px;\r\n}\r\n\r\nsection.samples .ms-Button, section.setup .ms-Button {\r\n    display: block;\r\n    margin-bottom: 5px;\r\n    margin-left: 20px;\r\n    min-width: 80px;\r\n}\r\n"
    language: css
libraries: |
    // Office.js
    https://appsforoffice.microsoft.com/lib/1/hosted/office.js

    // CSS Libraries
    office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
    office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css

    // NPM libraries
    core-js@2.4.1/client/core.min.js
    @microsoft/office-js-helpers@0.7.1/dist/office.helpers.min.js
    jquery@3.1.1

    // IntelliSense: @types/library or node_modules paths or URL to d.ts files
    @types/office-js
    @types/core-js
    @microsoft/office-js-helpers@0.7.1/dist/office.helpers.d.ts
    @types/jquery