Angular Spreadsheet Chart Adapter
El componente Angular Spreadsheet permite mostrar gráficos en su IgxSpreadsheetComponent
.
Ejemplo de adaptador de gráfico de hoja de cálculo Angular
EXAMPLE
DATA
MODULES
TS
HTML
SCSS
import { saveAs } from "file-saver" ;
import { Workbook } from "igniteui-angular-excel" ;
import { WorkbookFormat } from "igniteui-angular-excel" ;
import { WorkbookSaveOptions } from "igniteui-angular-excel" ;
export class ExcelUtility {
public static getExtension (format: WorkbookFormat ) {
switch (format) {
case WorkbookFormat.StrictOpenXml:
case WorkbookFormat.Excel2007:
return ".xlsx" ;
case WorkbookFormat.Excel2007MacroEnabled:
return ".xlsm" ;
case WorkbookFormat.Excel2007MacroEnabledTemplate:
return ".xltm" ;
case WorkbookFormat.Excel2007Template:
return ".xltx" ;
case WorkbookFormat.Excel97To2003:
return ".xls" ;
case WorkbookFormat.Excel97To2003Template:
return ".xlt" ;
}
}
public static load(file: File): Promise <Workbook> {
return new Promise <Workbook>((resolve, reject ) => {
ExcelUtility.readFileAsUint8Array(file).then((a ) => {
Workbook.load(a, null , (w ) => {
resolve(w);
}, (e ) => {
reject(e);
});
}, (e ) => {
reject(e);
});
});
}
public static loadFromUrl(url: string ): Promise <Workbook> {
return new Promise <Workbook>((resolve, reject ) => {
const req = new XMLHttpRequest();
req.open("GET" , url, true );
req.responseType = "arraybuffer" ;
req.onload = (d ) => {
const data = new Uint8Array (req.response);
Workbook.load(data, null , (w ) => {
resolve(w);
}, (e ) => {
reject(e);
});
};
req.send();
});
}
public static save(workbook: Workbook, fileNameWithoutExtension : string ): Promise <string > {
return new Promise <string >((resolve, reject ) => {
const opt = new WorkbookSaveOptions();
opt.type = "blob" ;
workbook.save(opt, (d ) => {
const fileExt = ExcelUtility.getExtension(workbook.currentFormat);
const fileName = fileNameWithoutExtension + fileExt;
saveAs(d as Blob, fileName);
resolve(fileName);
}, (e ) => {
reject(e);
});
});
}
private static readFileAsUint8Array(file: File): Promise <Uint8Array > {
return new Promise <Uint8Array >((resolve, reject ) => {
const fr = new FileReader();
fr.onerror = (e ) => {
reject(fr.error);
};
if (fr.readAsBinaryString) {
fr.onload = (e ) => {
const rs = (fr as any ).resultString;
const str: string = rs != null ? rs : fr.result;
const result = new Uint8Array (str.length);
for (let i = 0 ; i < str.length; i++) {
result[i] = str.charCodeAt(i);
}
resolve(result);
};
fr.readAsBinaryString(file);
} else {
fr.onload = (e ) => {
resolve(new Uint8Array (fr.result as ArrayBuffer ));
};
fr.readAsArrayBuffer(file);
}
});
}
}
ts コピー import { NgModule } from "@angular/core" ;
import { FormsModule } from "@angular/forms" ;
import { CommonModule } from "@angular/common" ;
import { BrowserModule } from "@angular/platform-browser" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import { ExcelUtility } from "./ExcelUtility" ;
import { IgxExcelModule } from "igniteui-angular-excel" ;
import { IgxSpreadsheetModule } from "igniteui-angular-spreadsheet" ;
import { IgxSpreadsheetChartAdapterModule } from "igniteui-angular-spreadsheet-chart-adapter" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
],
imports : [
BrowserModule,
BrowserAnimationsModule,
CommonModule,
FormsModule,
IgxExcelModule,
IgxSpreadsheetModule,
IgxSpreadsheetChartAdapterModule
],
providers : [ExcelUtility],
schemas : []
})
export class AppModule {}
ts コピー import { Component, OnInit, ViewChild } from "@angular/core" ;
import { ExcelUtility } from "./ExcelUtility" ;
import { IgxSpreadsheetComponent } from "igniteui-angular-spreadsheet" ;
import { SpreadsheetChartAdapter } from "igniteui-angular-spreadsheet-chart-adapter" ;
import { ChartTitle, ChartType, FormattedString, Workbook } from "igniteui-angular-excel" ;
import { Worksheet } from "igniteui-angular-excel" ;
import { WorksheetCell } from "igniteui-angular-excel" ;
@Component ({
standalone : false ,
selector : "app-root" ,
styleUrls : ["./app.component.scss" ],
templateUrl : "./app.component.html"
})
export class AppComponent implements OnInit {
@ViewChild ("spreadsheet" , { read : IgxSpreadsheetComponent, static : true })
public spreadsheet: IgxSpreadsheetComponent;
constructor ( ) { }
public ngOnInit ( ) {
this .spreadsheet.chartAdapter = new SpreadsheetChartAdapter();
const excelFile = "https://static.infragistics.com/xplatform/excel/ChartData.xlsx" ;
ExcelUtility.loadFromUrl(excelFile).then((w ) => {
this .spreadsheet.workbook = w;
const sheet: Worksheet = this .spreadsheet.workbook.worksheets(0 );
sheet.defaultColumnWidth = 500 * 20 ;
sheet.rows(0 ).height = 150 * 20 ;
const cell1: WorksheetCell = sheet.getCell("A1" );
const cell2: WorksheetCell = sheet.getCell("B1" );
const cell3: WorksheetCell = sheet.getCell("C1" );
const cell4: WorksheetCell = sheet.getCell("D1" );
const dataCellAddress = "A3:D6" ;
const chart1 = sheet.shapes().addChart(ChartType.Line, cell1, { x : 0 , y : 0 }, cell1, { x : 100 , y : 100 });
const title: ChartTitle = new ChartTitle();
title.text = new FormattedString("Line Chart" );
chart1.chartTitle = title;
chart1.setSourceData(dataCellAddress, true );
const chart2 = sheet.shapes().addChart(ChartType.ColumnClustered, cell2,
{ x : 0 , y : 0 }, cell2, { x : 100 , y : 100 });
const title2: ChartTitle = new ChartTitle();
title2.text = new FormattedString("Column Chart" );
chart2.chartTitle = title2;
chart2.setSourceData(dataCellAddress, true );
const chart3 = sheet.shapes().addChart(ChartType.Area, cell3, { x : 0 , y : 0 }, cell3, { x : 100 , y : 100 });
const title3: ChartTitle = new ChartTitle();
title3.text = new FormattedString("Area Chart" );
chart3.chartTitle = title3;
chart3.setSourceData(dataCellAddress, true );
const chart4 = sheet.shapes().addChart(ChartType.Pie, cell4, { x : 0 , y : 0 }, cell4, { x : 100 , y : 100 });
const title4: ChartTitle = new ChartTitle();
title4.text = new FormattedString("Pie Chart" );
chart4.chartTitle = title4;
chart4.setSourceData(dataCellAddress, true );
});
}
}
ts コピー <div class ="container vertical" >
<igx-spreadsheet #spreadsheet height ="100%" width ="100%" >
</igx-spreadsheet >
</div >
html コピー
¿Te gusta esta muestra? Obtenga acceso a nuestro kit de herramientas de Ignite UI for Angular completo y comience a crear sus propias aplicaciones en minutos. Descárgalo gratis.
Descripción general del adaptador de gráficos
Con chartAdapter
puede mostrar los gráficos en la hoja de cálculo. Los adaptadores de gráficos de hoja de cálculo crean e inicializan elementos de gráfico para la hoja de cálculo basándose en una instancia de Infragistics.Documents.Excel.WorksheetChart.
Para agregar un WorksheetChart a una hoja de trabajo, debe usar el método addChart
de la colección Shapes de la hoja de trabajo. Puede encontrar más detalles sobre cómo agregar gráficos en Excel a continuación.
Aquí está la descripción paso a paso:
Agregue la referencia SpreadsheetChartAdapterModule a su proyecto
Cree una instancia de una clase SpreadsheetChartAdapter asignándola a la hoja de cálculo
Ejecute su aplicación y cargue una hoja de trabajo que contenga un gráfico.
Tipos de gráficos admitidos
Hay más de 35 tipos de gráficos admitidos por los ChartAdapters de hoja de cálculo, incluidos Línea, Área, Columna y Anillo. Vea la lista completa aquí:
Gráficos de columnas
Columna agrupada
columna apilada
Columna 100% apilada
Gráficos de líneas
Línea
Línea con marcadores
línea apilada
Línea apilada con marcadores
Línea 100% apilada
Línea 100% apilada con marcadores.
Gráficos circulares
Gráficos de anillos
Gráfica de barras
barra agrupada
barra apilada
barra 100% apilada
Gráficos de área
Área
Área apilada
Área 100% apilada
Gráficos XY (dispersión) y de burbujas
Dispersión (solo con marcador)
Dispersar con líneas suaves
Dispersión con líneas suaves y marcadores.
Dispersión con líneas rectas
Dispersión con líneas rectas y marcadores.
Burbuja (sin efectos)
Efecto Burbuja3DE
Gráficos de acciones
Cierre alto-bajo
Abrir-alto-bajo-cerrar
Volumen-alto-bajo-cierre
Volumen-apertura-alto-bajo-cierre
Gráficos de radar
Radar sin marcadores
Radar con marcadores
Radar lleno
Gráficos combinados
Gráfico de columnas y líneas que comparten xAxis
Gráfico de columnas y líneas y segundo eje x
Área y columna apiladas
Combinación personalizada
dependencias
En el siguiente fragmento de código, se utiliza una clase ExcelUtility externa para guardar y cargar un libro.
Al configurar su control de hoja de cálculo Angular para agregar gráficos, deberá importar la clase SpreadsheetChartAdapter
de la siguiente manera:
import { IgxSpreadsheetChartAdapterModule } from 'igniteui-angular-spreadsheet-chart-adapter' ;
import { SpreadsheetChartAdapter } from 'igniteui-angular-spreadsheet-chart-adapter' ;
import { ChartTitle, ChartType, FormattedString, Workbook } from 'igniteui-angular-excel' ;
import { ExcelUtility } from "ExcelUtility" ;
import { Worksheet } from 'igniteui-angular-excel' ;
import { WorksheetCell } from 'igniteui-angular-excel' ;
ts
Fragmento de código
En el siguiente fragmento de código se muestra cómo agregar gráficos a la hoja de cálculo que se está viendo actualmente en el IgxSpreadsheetComponent
control:
this .spreadsheet.chartAdapter = new SpreadsheetChartAdapter();
ExcelUtility.loadFromUrl(process.env.PUBLIC_URL + "/ExcelFiles/ChartData.xlsx" ).then((w ) => {
this .spreadsheet.workbook = w;
const sheet: Worksheet = this .spreadsheet.workbook.worksheets(0 );
sheet.defaultColumnWidth = 500 * 20 ;
sheet.rows(0 ).height = 150 * 20 ;
const cell1: WorksheetCell = sheet.getCell("A1" );
const cell2: WorksheetCell = sheet.getCell("B1" );
const cell3: WorksheetCell = sheet.getCell("C1" );
const cell4: WorksheetCell = sheet.getCell("D1" );
const dataCellAddress = "A4:D6" ;
const chart1 = sheet.shapes().addChart(ChartType.Line, cell1, { x : 0 , y : 0 }, cell1, { x : 100 , y : 100 });
const title: Angular ChartTitle = new ChartTitle();
title.text = new FormattedString("Line Chart" );
chart1.chartTitle = title;
chart1.setSourceData(dataCellAddress, true );
const chart2 = sheet.shapes().addChart(ChartType.ColumnClustered, cell2, { x : 0 , y : 0 }, cell2, { x : 100 , y : 100 });
const title2: ChartTitle = new ChartTitle();
title2.text = new FormattedString("Column Chart" );
chart2.chartTitle = title2;
chart2.setSourceData(dataCellAddress, true );
const chart3 = sheet.shapes().addChart(ChartType.Area, cell3, { x : 0 , y : 0 }, cell3, { x : 100 , y : 100 });
const title3: ChartTitle = new ChartTitle();
title3.text = new FormattedString("Area Chart" );
chart3.chartTitle = title3;
chart3.setSourceData(dataCellAddress, true );
const chart4 = sheet.shapes().addChart(ChartType.Pie, cell4, { x : 0 , y : 0 }, cell4, { x : 100 , y : 100 });
const title4: ChartTitle = new ChartTitle();
title4.text = new FormattedString("Pie Chart" );
chart4.chartTitle = title4;
chart4.setSourceData(dataCellAddress, true );
});
typescript
Referencias de API