dees-catalog/ts_web/elements/dees-chart-area.ts

193 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-02-03 10:26:15 +00:00
import {
DeesElement,
css,
cssManager,
customElement,
html,
property,
2024-02-03 13:42:20 +00:00
state,
2024-02-03 10:26:15 +00:00
type CSSResult,
type TemplateResult,
} from '@design.estate/dees-element';
import * as domtools from '@design.estate/dees-domtools';
import { demoFunc } from './dees-chart-area.demo.js';
import ApexCharts from 'apexcharts';
declare global {
interface HTMLElementTagNameMap {
'dees-chart-area': DeesChartArea;
}
}
@customElement('dees-chart-area')
export class DeesChartArea extends DeesElement {
public static demo = demoFunc;
2024-02-03 13:42:20 +00:00
// instance
@state()
public chart: ApexCharts;
@property()
2024-02-03 13:49:25 +00:00
public label: string = 'Untitled Chart';
2024-02-03 13:42:20 +00:00
2024-02-03 10:26:15 +00:00
constructor() {
super();
domtools.elementBasic.setup();
}
2024-02-03 13:42:20 +00:00
public static styles = [
cssManager.defaultStyles,
css`
:host {
font-family: 'Roboto', sans-serif;
color: #ccc;
font-weight: 600;
font-size: 12px;
}
.mainbox {
position: relative;
width: 100%;
max-width: 600px;
height: 400px;
background: #222;
border-radius: 8px;
padding: 32px 16px 16px 0px;
}
.chartTitle {
position: absolute;
top: 0;
left: 0;
width: 100%;
text-align: center;
padding-top: 16px;
}
.chartContainer {
position: relative;
width: 100%;
height: 100%;
}
`,
];
2024-02-03 10:26:15 +00:00
public render(): TemplateResult {
2024-02-03 13:42:20 +00:00
return html` <div class="mainbox">
2024-02-03 13:49:25 +00:00
<div class="chartTitle">${this.label}</div>
2024-02-03 13:42:20 +00:00
<div class="chartContainer"></div>
</div> `;
2024-02-03 10:26:15 +00:00
}
public async firstUpdated() {
2024-02-03 13:42:20 +00:00
const domtoolsInstance = await this.domtoolsPromise;
var options: ApexCharts.ApexOptions = {
2024-02-03 10:26:15 +00:00
series: [
{
2024-02-03 13:42:20 +00:00
name: 'cpu',
2024-02-03 10:26:15 +00:00
data: [31, 40, 28, 51, 42, 109, 100],
},
{
2024-02-03 13:42:20 +00:00
name: 'memory',
2024-02-03 10:26:15 +00:00
data: [11, 32, 45, 32, 34, 52, 41],
},
],
chart: {
2024-02-03 13:42:20 +00:00
width: 0, // Adjusted for responsive width
height: 0, // Adjusted for responsive height
2024-02-03 10:26:15 +00:00
type: 'area',
2024-02-03 13:42:20 +00:00
toolbar: {
show: false, // This line disables the toolbar
},
2024-02-03 10:26:15 +00:00
},
dataLabels: {
enabled: false,
},
stroke: {
2024-02-03 13:42:20 +00:00
width: 1,
2024-02-03 10:26:15 +00:00
curve: 'smooth',
},
xaxis: {
2024-02-03 13:42:20 +00:00
crosshairs: {
stroke: {
width: 1,
color: '#444',
},
},
2024-02-03 10:26:15 +00:00
type: 'datetime',
categories: [
'2018-09-19T00:00:00.000Z',
'2018-09-19T01:30:00.000Z',
'2018-09-19T02:30:00.000Z',
'2018-09-19T03:30:00.000Z',
'2018-09-19T04:30:00.000Z',
'2018-09-19T05:30:00.000Z',
'2018-09-19T06:30:00.000Z',
],
},
2024-02-03 13:42:20 +00:00
yaxis: {
crosshairs: {
stroke: {
width: 1,
color: '#444',
},
},
},
2024-02-03 10:26:15 +00:00
tooltip: {
x: {
format: 'dd/MM/yy HH:mm',
},
},
2024-02-03 13:42:20 +00:00
grid: {
xaxis: {
lines: {
show: true, // This enables the grid lines along the x-axis
},
},
yaxis: {
lines: {
show: true,
},
},
borderColor: '#666', // Set the color of the grid lines
strokeDashArray: 2, // Solid line
row: {
colors: [], // This can be used to alternate the shading of the horizontal rows
opacity: 0.1,
},
column: {
colors: [], // For vertical column bands, not needed here but available for customization
opacity: 0.1,
},
},
2024-02-03 10:26:15 +00:00
};
2024-02-03 13:42:20 +00:00
this.chart = new ApexCharts(this.shadowRoot.querySelector('.chartContainer'), options);
await this.chart.render();
await this.resizeChart();
}
public async resizeChart() {
const element = this.shadowRoot.querySelector('.chartContainer');
// Get computed style of the element
const style = window.getComputedStyle(element);
2024-02-03 10:26:15 +00:00
2024-02-03 13:42:20 +00:00
// Extract padding values
const paddingTop = parseInt(style.paddingTop, 10);
const paddingBottom = parseInt(style.paddingBottom, 10);
const paddingLeft = parseInt(style.paddingLeft, 10);
const paddingRight = parseInt(style.paddingRight, 10);
// Calculate the actual width and height to use, subtracting padding
const actualWidth = element.clientWidth - paddingLeft - paddingRight;
const actualHeight = element.clientHeight - paddingTop - paddingBottom;
await this.chart.updateOptions({
chart: {
width: actualWidth,
height: actualHeight,
},
});
2024-02-03 10:26:15 +00:00
}
}