| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- define(['jquery', 'bootstrap', 'backend', 'addtabs', 'echarts', 'echarts-theme'], function ($, undefined, Backend, undefined, Echarts) {
- var Controller = {
- index: function () {
- var procChart = null;
- var chartData = Config.procuremenChart || {};
- var chartEl = document.getElementById('procuremen-echart');
- function toNumArr(arr) {
- if (!$.isArray(arr)) {
- return [];
- }
- return arr.map(function (v) {
- var n = parseInt(v, 10);
- return isNaN(n) || n < 0 ? 0 : n;
- });
- }
- function buildOption() {
- var labels = chartData.labels || [];
- var seriesMeta = [
- {name: '待确认', key: 'confirm', color: '#f0ad4e'},
- {name: '待审批', key: 'approval', color: '#7266ba'},
- {name: '已完结', key: 'completed', color: '#27C24C'}
- ];
- var maxVal = 0;
- var seriesData = {};
- seriesMeta.forEach(function (meta) {
- var nums = toNumArr(chartData[meta.key]);
- seriesData[meta.key] = nums;
- nums.forEach(function (n) {
- if (n > maxVal) {
- maxVal = n;
- }
- });
- });
- // 顶部留白,避免折线贴顶;至少显示到 2,刻度更易读
- var yMax = maxVal <= 0 ? 2 : Math.max(maxVal + 1, Math.ceil(maxVal * 1.2));
- return {
- color: seriesMeta.map(function (m) {
- return m.color;
- }),
- title: {
- text: '近7天单数',
- left: 8,
- top: 8,
- textStyle: {
- fontSize: 13,
- fontWeight: 600,
- color: '#555'
- }
- },
- tooltip: {
- trigger: 'axis',
- axisPointer: {type: 'line', lineStyle: {color: '#ddd'}},
- backgroundColor: 'rgba(50,50,50,0.92)',
- borderWidth: 0,
- textStyle: {fontSize: 12}
- },
- legend: {
- data: seriesMeta.map(function (m) {
- return m.name;
- }),
- top: 8,
- right: 12,
- itemWidth: 10,
- itemHeight: 10,
- textStyle: {fontSize: 11}
- },
- grid: {
- left: 16,
- right: 20,
- top: 44,
- bottom: 16,
- containLabel: true
- },
- xAxis: {
- type: 'category',
- boundaryGap: false,
- data: labels,
- axisTick: {show: false},
- axisLabel: {
- fontSize: 11,
- color: '#888',
- margin: 8,
- interval: 0
- },
- axisLine: {lineStyle: {color: '#e8e8e8'}}
- },
- yAxis: {
- type: 'value',
- min: 0,
- max: yMax,
- minInterval: 1,
- splitNumber: Math.min(5, yMax),
- axisLabel: {
- fontSize: 11,
- color: '#888',
- formatter: function (v) {
- return parseInt(v, 10) || 0;
- }
- },
- splitLine: {lineStyle: {color: '#f0f0f0', type: 'dashed'}}
- },
- series: seriesMeta.map(function (meta) {
- return {
- name: meta.name,
- type: 'line',
- // 轻度平滑:避免 true 时稀疏数据贝塞尔过冲(贴底/右上尖刺)
- smooth: 0.25,
- clip: true,
- showSymbol: true,
- symbol: 'circle',
- symbolSize: 6,
- connectNulls: false,
- lineStyle: {width: 2, color: meta.color},
- itemStyle: {color: meta.color, borderWidth: 2, borderColor: '#fff'},
- emphasis: {focus: 'series', lineStyle: {width: 3}},
- data: seriesData[meta.key]
- };
- })
- };
- }
- function safeResize() {
- if (!procChart || !chartEl) {
- return;
- }
- var w = chartEl.clientWidth;
- var h = chartEl.clientHeight;
- if (w < 40 || h < 40) {
- return;
- }
- procChart.resize({width: w, height: h});
- }
- function renderChart() {
- if (!chartEl) {
- return;
- }
- if (!procChart) {
- procChart = Echarts.init(chartEl, 'walden');
- }
- procChart.setOption(buildOption(), true);
- safeResize();
- }
- if (chartEl) {
- renderChart();
- }
- $(window).on('resize.procuremenDash', function () {
- safeResize();
- });
- // 侧栏折叠 / 标签切换后容器尺寸变化,需再 resize
- $(document).on('expanded.pushMenu collapsed.pushMenu', function () {
- setTimeout(safeResize, 280);
- });
- $(document).on('shown.bs.tab click', '.nav-addtabs a, [addtabs]', function () {
- setTimeout(safeResize, 120);
- });
- setTimeout(safeResize, 120);
- setTimeout(safeResize, 400);
- setTimeout(function () {
- // 尺寸仍异常时强制按容器重绘一次
- if (chartEl && procChart && (chartEl.clientHeight < 80 || chartEl.clientWidth < 80)) {
- renderChart();
- } else {
- safeResize();
- }
- }, 800);
- $(document).on('click', '.btn-refresh', function () {
- setTimeout(safeResize, 0);
- });
- }
- };
- return Controller;
- });
|