Office.js Beta版-如何创建旭日形图

时间:2019-01-09 18:04:41

标签: office-js

测试版表明支持新图表:“探索我们支持的新图表类型:地图,箱形和晶须,瀑布,朝阳,pareto和漏斗”

我想举一个如何使用图表对象创建新的旭日形图的示例

1 个答案:

答案 0 :(得分:0)

async function createSunburstChart() {
  try {
    await Excel.run(async (context) => {
      await OfficeHelpers.ExcelUtilities.forceCreateSheet(context.workbook, "Sunburst");
      let sheet = context.workbook.worksheets.getItem("Sunburst");
      sheet.activate();
      sheet.showGridlines = false;
      //const sheet = context.workbook.worksheets.getActiveWorksheet();

      // prepare table data for waterfall chart
      // let expensesTable = sheet.tables.getItemOrNullObject("sunburst");
      // expensesTable.load("name");
      // await context.sync();
      // if (expensesTable.name == null)
      // {
      let expensesTable = sheet.tables.add("A1:C1", true);
      expensesTable.name = "sunburst";
      expensesTable.getHeaderRowRange().values = [["Category", "Sub-Category", "Sales"]];

      expensesTable.rows.add(null, [
        ["Furniture", "Furnishings", 560.32],
        ["", "Tables", 3123.518],
        ["", "Bookcases", 66.586],
        ["", "Furnishings", 560.32],
        ["", "Chairs", 1427.22],
        ["Office Supplies", "Storage", 902.28],
        ["", "Art", 306.776],
        ["", "Labels", 100.714],
        ["", "Paper", 255.806],
        ["", "Fasteners", 33.776],
        ["", "Binders", 236.668],
        ["", "Envelopes", 128.578],
        ["", "Appliances", 917.702],
        ["", "Supplies", 32.992],
        ["Technology", "Accessories", 1532.03],
        ["", "Phones", 3949.406],
        ["", "Accessories", 1532.03],
        ["", "Machines", 0]
      ]);
      // }

      //end of prepare table data for waterfall chart

      let bwRange = sheet.getRange("A1:C19");

      //let chart = sheet.charts.getItemOrNullObject("sunburst");
      // chart.load("name");
      // await context.sync();
      // if (chart.name == null) {
      let chart = sheet.charts.add("Sunburst", bwRange, "auto");
      chart.setPosition("E2:L19");
      chart.name = "sunburst";
      //}
      chart.series.getItemAt(0).hasDataLabels = true;
      await context.sync();
    });
  } catch (error) {
    OfficeHelpers.UI.notify(error);
    OfficeHelpers.Utilities.log(error);
  }
}