Commit 3a926a8f authored by Kevin Yumang's avatar Kevin Yumang

SERV-2286 - linked backend/frontend data to js file. (parameters) WIP

parent ef8a4d9e
...@@ -22,13 +22,16 @@ function init_chart(chartId) { ...@@ -22,13 +22,16 @@ function init_chart(chartId) {
if (lineChartElement) { if (lineChartElement) {
let titles = ['A', 'B', 'C']; let elementObject = $(lineChartElement);
let multichart = (titles && titles.length > 1); let numberOfCharts = elementObject.attr('charts') ?? 1;
let numberOfCharts = titles ? titles.length : 1; let multichart = (numberOfCharts > 1);
console.log(numberOfCharts);
console.log(multichart);
// adjust container height before initialising the chart. // adjust container height before initialising the chart.
if (multichart) { if (multichart) {
let height = (titles.length * 240); // (chart height = 160 | charts spacing = 80) let height = (numberOfCharts * 240); // (chart height = 160 | charts spacing = 80)
lineChartElement.style.height = height + 'px'; lineChartElement.style.height = height + 'px';
console.log('adjusted container height', height + 'px'); console.log('adjusted container height', height + 'px');
} }
...@@ -43,7 +46,7 @@ function init_chart(chartId) { ...@@ -43,7 +46,7 @@ function init_chart(chartId) {
fontSize: 15 fontSize: 15
}); });
set_data(titles, multichart, numberOfCharts, function(options) { set_data(elementObject, multichart, numberOfCharts, function(options) {
lineChart.hideLoading(); lineChart.hideLoading();
lineChart.setOption(options); lineChart.setOption(options);
setResizeEvent(lineChartElement, lineChart); setResizeEvent(lineChartElement, lineChart);
...@@ -51,47 +54,57 @@ function init_chart(chartId) { ...@@ -51,47 +54,57 @@ function init_chart(chartId) {
} }
} }
function set_data(titles, multichart, numberOfCharts, callback) { function set_data(elementObject, multichart, numberOfCharts, callback) {
setTimeout(function() { setTimeout(function() {
const defaultColors = ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc', '#a1887f'];
// variables (this will be set by user) // variables (this will be set by user)
let color = ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc', '#a1887f']; let titles = JSON.parse(elementObject.attr('titles')) ?? [];
let animationDuration = 750; let colors = JSON.parse(elementObject.attr('colors')) ?? defaultColors;
let seriesNames = ['ONC', 'APR', 'SPR', 'PHX', 'SIG', 'BRG', 'MNT', 'SDK', 'OCT'];
let xAxisData = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; let showPointValues = (elementObject.attr('point-values') == 'true' || elementObject.attr('point-values') == true); // default false
let yAxisLabelValueFormat = '{value}'; // ex '{value} users' - https://echarts.apache.org/en/option.html#yAxis.axisLabel.formatter let boundaryGap = (elementObject.attr('boundary-gap') == 'true' || elementObject.attr('boundary-gap') == true); // default true
let showPointValues = false; let inverted = (elementObject.attr('inverted') == 'true' || elementObject.attr('inverted') == true); // default false
let boundaryGap = true; let showDataZoom = (elementObject.attr('data-zoom') == 'true' || elementObject.attr('data-zoom') == true); // default false
let inverted = false; let stacked = (elementObject.attr('stacked') == 'true' || elementObject.attr('stacked') == true); // default false
let showDataZoom = true;
let stacked = false; let animationDuration = elementObject.attr('animation-duration') ?? 750;
let markLine = 'average'; // min | max | average // ex '{value} users' - https://echarts.apache.org/en/option.html#yAxis.axisLabel.formatter
let series = []; let yAxisLabelValueFormat = elementObject.attr('y-axis-label-value-format') ?? '{value}';
// min | max | average
seriesNames.forEach(function (val, idx, arr) { let markLine = elementObject.attr('mark-line') ?? null;
let categories = JSON.parse(elementObject.attr('categories')) ?? null;
let chartindex = Math.floor(Math.random() * titles.length);
let series = JSON.parse(elementObject.attr('series')) ?? null;
series.push(setLineChartSeries(val, [ let seriesNames = series && series.map(function(item){ return item.name });
Math.floor(Math.random() * 100), let seriesObject = setSeries(series, showPointValues, markLine, stacked);
Math.floor(Math.random() * 100),
Math.floor(Math.random() * 100), console.log('titles', titles);
Math.floor(Math.random() * 100), console.log('colors', colors);
Math.floor(Math.random() * 100), console.log('point-values', showPointValues);
Math.floor(Math.random() * 100), console.log('boundary-gap', boundaryGap);
Math.floor(Math.random() * 100), console.log('inverted', inverted);
], showPointValues, markLine, stacked, chartindex)); console.log('data-zoom', showDataZoom);
}); console.log('stacked', stacked);
console.log('animation-duration', animationDuration);
console.log('y-axis-label-value-format', yAxisLabelValueFormat);
console.log('mark-line', markLine);
console.log('categories', categories);
console.log('series', series);
console.log('series names', seriesNames);
console.log('series object', seriesObject);
// Options // Options
let options = { let options = {
// Chart title // Chart title
title: setTitles(titles), title: setTitles(titles, numberOfCharts),
// Define colors // Define colors
color: color, color: colors,
// Global text styles // Global text styles
textStyle: setGlobalTextStyle(), textStyle: setGlobalTextStyle(),
...@@ -109,21 +122,22 @@ function set_data(titles, multichart, numberOfCharts, callback) { ...@@ -109,21 +122,22 @@ function set_data(titles, multichart, numberOfCharts, callback) {
tooltip: setTooltip(), tooltip: setTooltip(),
// Horizontal axis // Horizontal axis
xAxis: inverted ? setYAxis(yAxisLabelValueFormat, numberOfCharts) : setXAxis(xAxisData, boundaryGap, numberOfCharts), xAxis: inverted ? setYAxis(yAxisLabelValueFormat, numberOfCharts) : setXAxis(categories, boundaryGap, numberOfCharts),
// Vertical axis // Vertical axis
yAxis: inverted ? setXAxis(xAxisData, boundaryGap, numberOfCharts) : setYAxis(yAxisLabelValueFormat, numberOfCharts), yAxis: inverted ? setXAxis(categories, boundaryGap, numberOfCharts) : setYAxis(yAxisLabelValueFormat, numberOfCharts),
// Axis pointer // Axis pointer
axisPointer: setAxisPointer(multichart), axisPointer: setAxisPointer(multichart),
// Zoom control
dataZoom: setDataZoom(showDataZoom, multichart),
// Add series // Add series
series: series series: seriesObject
}; };
if(showDataZoom == true) {
options.dataZoom = setDataZoom(showDataZoom, multichart);
}
callback(options); callback(options);
}, 1000); }, 1000);
...@@ -131,40 +145,63 @@ function set_data(titles, multichart, numberOfCharts, callback) { ...@@ -131,40 +145,63 @@ function set_data(titles, multichart, numberOfCharts, callback) {
} }
// sub functions // sub functions
function setLineChartSeries(name, data = [], showPointValues = false, markLineDataType = null, stack = false, index = 0) { function setSeries(seriesData, showPointValues = false, markLineDataType = null, stack = false) {
let series = { // uses series options if available else use global options.
name: name,
type: 'line', let seriesArray = [];
data: data, for(let i = 0; i < seriesData.length; i++) {
stack: stack,
smooth: true, let series = {
symbol: 'circle', name: seriesData[i].name,
symbolSize: 7, type: seriesData[i].type ?? 'line',
label: { data: seriesData[i].data,
normal: { stack: seriesData[i].stack ?? stack,
show: showPointValues smooth: true,
} symbol: 'circle',
}, symbolSize: 7,
markLine: { label: {
data: [{ normal: {
type: markLineDataType, show: seriesData[i].showPointValues ?? showPointValues
name: markLineDataType !== null ? (markLineDataType[0].toUpperCase() + markLineDataType.substring(1)) : null, }
}] },
}, itemStyle: {
itemStyle: { normal: {
normal: { borderWidth: 2
borderWidth: 2 }
} },
}, xAxisIndex: seriesData[i].index ?? 0,
xAxisIndex: index, yAxisIndex: seriesData[i].index ?? 0
yAxisIndex: index }
if(seriesData[i].markline != undefined && ['min', 'max', 'average'].includes(markLineDataType)) {
series.markLine = {
data: [{
type: seriesData[i].markline,
name: seriesData[i].markline[0].toUpperCase() + seriesData[i].markline.substring(1),
}]
};
} else if(markLineDataType != null && ['min', 'max', 'average'].includes(markLineDataType)) {
series.markLine = {
data: [{
type: markLineDataType,
name: markLineDataType[0].toUpperCase() + markLineDataType.substring(1),
}]
};
}
seriesArray.push(series);
} }
return series; return seriesArray;
} }
function setXAxis(xAxisData, boundaryGap = true, numberOfCharts = 1) { function setXAxis(categories, boundaryGap = true, numberOfCharts = 1) {
let xAxisArray = []; let xAxisArray = [];
for (let i = 0; i < numberOfCharts; i++) { for (let i = 0; i < numberOfCharts; i++) {
...@@ -172,7 +209,6 @@ function setXAxis(xAxisData, boundaryGap = true, numberOfCharts = 1) { ...@@ -172,7 +209,6 @@ function setXAxis(xAxisData, boundaryGap = true, numberOfCharts = 1) {
let x = { let x = {
type: 'category', type: 'category',
boundaryGap: boundaryGap, boundaryGap: boundaryGap,
data: xAxisData,
axisLabel: { axisLabel: {
color: '#fff' color: '#fff'
}, },
...@@ -189,9 +225,15 @@ function setXAxis(xAxisData, boundaryGap = true, numberOfCharts = 1) { ...@@ -189,9 +225,15 @@ function setXAxis(xAxisData, boundaryGap = true, numberOfCharts = 1) {
gridIndex: i gridIndex: i
}; };
if(categories != null) {
x.data = categories;
}
xAxisArray.push(x); xAxisArray.push(x);
} }
console.log('xaxisarray', xAxisArray);
return xAxisArray; return xAxisArray;
} }
...@@ -243,17 +285,25 @@ function setLegend(legendData, multichart = false) { ...@@ -243,17 +285,25 @@ function setLegend(legendData, multichart = false) {
} }
} }
function setTitles(titles) { function setTitles(titles, numberOfCharts = 1) {
// if not array.
if(!Array.isArray(titles)) {
let temp = titles;
titles = [];
titles.push(temp);
}
let arrayTitles = []; let arrayTitles = [];
titles.forEach(function(val, idx, arr) {
for(i = 0; i < numberOfCharts; i++) {
let title = { let title = {
text: val, text: titles[i],
}; };
// if single else multichart // if single else multichart
if(idx == 0 && arr.length == 1) { if(i == 0 && numberOfCharts == 1) {
title.textStyle = { title.textStyle = {
color: '#fff' color: '#fff'
}; };
...@@ -264,11 +314,11 @@ function setTitles(titles) { ...@@ -264,11 +314,11 @@ function setTitles(titles) {
color: '#fff' color: '#fff'
}; };
title.left = 'center'; title.left = 'center';
title.top = 240 * idx; // (chart height = 160 | charts spacing = 80) * index title.top = 240 * i; // (chart height = 160 | charts spacing = 80) * index
} }
arrayTitles.push(title); arrayTitles.push(title);
}); }
console.log('titles', arrayTitles); console.log('titles', arrayTitles);
......
<div class="chart-container">
<div class="chart has-fixed-height LimitlessLineChart"
charts="{{ $charts }}"
titles="{{ json_encode($titles) }}"
colors="{{ json_encode($colors) }}"
point-values="{{ $pointValues }}"
boundary-gap="{{ $boundaryGap }}"
inverted="{{ $inverted }}"
data-zoom="{{ $dataZoom }}"
stacked="{{ $stacked }}"
animation-duration="{{ $animationDuration }}"
@if($yAxisLabelValueFormat != null) y-axis-label-value-format="{{ $yAxisLabelValueFormat }}" @endif
@if($markLine != null) mark-line="{{ $markLine }}" @endif
categories="{{ json_encode($categories) }}"
series="{{ json_encode($series) }}"
>
...@@ -32,8 +32,7 @@ ...@@ -32,8 +32,7 @@
class Config class Config
{ {
public $methodAllocation = [ public $methodAllocation = [
'Limitless::LineChartStart' => 'lineChartStart', 'Limitless::Chart' => 'chart'
'Limitless::LineChartStop' => 'LineChartStop',
]; ];
public $assetAllocation; public $assetAllocation;
...@@ -41,7 +40,7 @@ ...@@ -41,7 +40,7 @@
public function __construct() public function __construct()
{ {
$this->assetAllocation = [ $this->assetAllocation = [
'Limitless::LineChartStart' => [ 'Limitless::Chart' => [
'Attachments/linechart.init.js', 'Attachments/linechart.init.js',
secure_url('/ceetrox/sidekick/resource/public/Webdesigns/design-limitless/Template/global_assets/js/plugins/visualization/echarts/echarts.min.js'), secure_url('/ceetrox/sidekick/resource/public/Webdesigns/design-limitless/Template/global_assets/js/plugins/visualization/echarts/echarts.min.js'),
] ]
...@@ -51,24 +50,25 @@ ...@@ -51,24 +50,25 @@
/* /*
|-------------------------------------------------------------------------------------------- |--------------------------------------------------------------------------------------------
| Method "lineChartStart" | Method "chart"
|-------------------------------------------------------------------------------------------- |--------------------------------------------------------------------------------------------
*/ */
public function lineChartStart($parameters) public function chart($parameters)
{ {
return View('Limitless::Linechart.Start'); return View('Limitless::Linechart.Chart')
->withCharts($parameters['charts'] ?? 1)
} ->withTitles($parameters['titles'] ?? null)
->withColors($parameters['colors'] ?? null)
->withPointValues($parameters['point-values'] ?? false)
/* ->withBoundaryGap($parameters['boundary-gap'] ?? true)
|-------------------------------------------------------------------------------------------- ->withInverted($parameters['inverted'] ?? false)
| Method "LineChartStop" ->withDataZoom($parameters['data-zoom'] ?? false)
|-------------------------------------------------------------------------------------------- ->withStacked($parameters['stacked'] ?? false)
*/ ->withAnimationDuration($parameters['animation-duration'] ?? 750)
public function LineChartStop($parameters) ->withYAxisLabelValueFormat($parameters['y-axis-label-value-format'] ?? null)
{ ->withMarkLine($parameters['mark-line'] ?? null)
return View('Limitless::Linechart.Stop'); ->withCategories($parameters['categories'] ?? null)
->withSeries($parameters['series'] ?? null);;
} }
......
...@@ -3,16 +3,52 @@ ...@@ -3,16 +3,52 @@
@section('Limitless::Content') @section('Limitless::Content')
@php
// sample backend data
$categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
$series = ['ONC', 'APR', 'SPR', 'PHX', 'SIG', 'BRG', 'MNT', 'SDK', 'OCT'];
$seriesData = [];
foreach ($series as $s) {
$data['name'] = $s;
$dataArray = [];
foreach ($categories as $c) {
$dataArray[] = rand(1,100);
}
$data['data'] = $dataArray;
$data['index'] = 0; //rand(0,1);
$seriesData[] = $data;
}
@endphp
@Limitless::CardStart(['title' => "Description", 'icon' => 'icon-info22' ] ) @Limitless::CardStart(['title' => "Description", 'icon' => 'icon-info22' ] )
@Limitless::LineChartStart @Limitless::Chart([
'titles' => 'Sample 1',
'charts' => 1,
'colors' => ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc', '#a1887f'],
'point-values' => true,
'boundary-gap' => true,
'inverted' => true,
'data-zoom' => true,
'stacked' => true,
'animation-duration' => 1000,
'y-axis-label-value-format' => '{value} users',
'mark-line' => 'average',
'categories' => $categories,
'series' => $seriesData
])
@Limitless::CardStop @Limitless::CardStop
@Limitless::CardStart(['title' => "Chart 2", 'icon' => 'icon-info22' ] ) {{-- @Limitless::CardStart(['title' => "Chart 2", 'icon' => 'icon-info22' ] )--}}
@Limitless::LineChartStart {{-- @Limitless::Chart--}}
@Limitless::CardStop {{-- @Limitless::CardStop--}}
@Limitless::CardStart(['title' => "Chart 3", 'icon' => 'icon-info22' ] ) {{-- @Limitless::CardStart(['title' => "Chart 3", 'icon' => 'icon-info22' ] )--}}
@Limitless::LineChartStart {{-- @Limitless::LineChartStart--}}
@Limitless::CardStop {{-- @Limitless::LineChartStop--}}
{{-- @Limitless::CardStop--}}
@stop @stop
<div class="chart-container">
<div class="chart has-fixed-height LimitlessLineChart">
</div>
</div>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment