diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index b903da21..d07963b3 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -15,55 +15,29 @@ use App\Services\Repositories\UserRepository; class AdminController extends Controller { - public function index() + public function chartData() { $today = Carbon::today()->timestamp; + $xAxis = []; + $userRegistration = []; + $textureUploads = []; - // Prepare data for the graph - $data = []; - $labels = []; - - for ($i = 6; $i >= 0; $i--) { + for ($i = 30; $i >= 0; $i--) { $time = Carbon::createFromTimestamp($today - $i * 86400); - $labels[] = $time->format('m-d'); - $data['user_registration'][] = User::like('register_at', $time->toDateString())->count(); - $data['texture_uploads'][] = Texture::like('upload_at', $time->toDateString())->count(); + $xAxis[] = $time->format('m-d'); + $userRegistration[] = User::like('register_at', $time->toDateString())->count(); + $textureUploads[] = Texture::like('upload_at', $time->toDateString())->count(); } - $datasets = [ - [ - 'label' => trans('admin.index.user-registration'), - 'backgroundColor' => 'rgba(60, 141, 188, 0.6)', - 'borderColor' => '#3c8dbc', - 'pointRadius' => 0, - 'pointBorderColor' => '#3c8dbc', - 'pointBackgroundColor' => '#3c8dbc', - 'pointHoverBackgroundColor' => '#3c8dbc', - 'pointHoverBorderColor' => '#3c8dbc', - 'data' => $data['user_registration'], - ], - [ - 'label' => trans('admin.index.texture-uploads'), - 'backgroundColor' => 'rgba(210, 214, 222, 0.6)', - 'borderColor' => '#d2d6de', - 'pointRadius' => 0, - 'pointBorderColor' => '#c1c7d1', - 'pointBackgroundColor' => '#c1c7d1', - 'pointHoverBackgroundColor' => '#c1c7d1', - 'pointHoverBorderColor' => '#c1c7d1', - 'data' => $data['texture_uploads'], + return [ + 'labels' => [ + trans('admin.index.user-registration'), + trans('admin.index.texture-uploads') ], + 'xAxis' => $xAxis, + 'data' => [$userRegistration, $textureUploads] ]; - - $options = [ - 'tooltips' => [ - 'intersect' => false, - 'mode' => 'index', - ], - ]; - - return view('admin.index', ['chartOptions' => compact('labels', 'datasets', 'options')]); } public function customize(Request $request) diff --git a/package.json b/package.json index 91d22386..a71c57e5 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "@babel/runtime": "^7.3.4", "@fortawesome/fontawesome-free": "^5.7.2", "admin-lte": "^2.4.10", - "chart.js": "^2.7.3", + "echarts": "^4.1.0", "icheck": "^1.0.2", "jquery": "^3.3.1", "skinview3d": "^1.1.0", @@ -38,6 +38,7 @@ "@babel/preset-env": "^7.3.4", "@babel/preset-typescript": "^7.3.3", "@gplane/tsconfig": "^1.0.0", + "@types/echarts": "^4.1.7", "@types/jest": "^24.0.11", "@types/jquery": "^3.3.9", "@types/toastr": "^2.1.36", @@ -155,7 +156,13 @@ "no-unused-vars": 0, "no-invalid-this": 0, "indent": 0, - "@typescript-eslint/indent": [2, 2, { "SwitchCase": 1 }] + "@typescript-eslint/indent": [ + 2, + 2, + { + "SwitchCase": 1 + } + ] } } ] diff --git a/resources/assets/src/views/admin/Dashboard.ts b/resources/assets/src/views/admin/Dashboard.ts new file mode 100644 index 00000000..731f0a35 --- /dev/null +++ b/resources/assets/src/views/admin/Dashboard.ts @@ -0,0 +1,95 @@ +import echarts from 'echarts/lib/echarts' +import 'echarts/lib/chart/line' +import 'echarts/lib/component/dataZoom' +import 'echarts/lib/component/legend' +import 'echarts/lib/component/tooltip' +import { get } from '../../js/net' + +interface ChartData { + labels: string[] + xAxis: string[] + data: number[][] +} + +async function createChart(el: HTMLCanvasElement) { + const chart = echarts.init(el) + chart.setOption({ + tooltip: { + trigger: 'axis', + axisPointer: { + type: 'cross', + label: { + backgroundColor: '#6a7985', + }, + }, + }, + dataZoom: [ + { type: 'inside', start: 75 }, + { type: 'slider', start: 75 }, + ], + legend: { + data: [], + }, + xAxis: [ + { + type: 'category', + boundaryGap: false, + data: [], + }, + ], + yAxis: [ + { + type: 'category', + boundaryGap: false, + }, + ], + series: [ + { + name: '', + type: 'line', + stack: '', + areaStyle: {}, + data: [], + }, + { + name: '', + type: 'line', + stack: '', + areaStyle: {}, + data: [], + }, + ], + }) + + const chartData: ChartData = await get('/admin/chart') + chart.setOption({ + legend: { + data: chartData.labels, + }, + xAxis: [ + { + type: 'category', + boundaryGap: false, + data: chartData.xAxis, + }, + ], + series: chartData.labels.map( + (label: string, index: number): echarts.EChartOption.SeriesLine => ({ + name: label, + type: 'line', + stack: 'total', + areaStyle: {}, + data: chartData.data[index], + smooth: true, + symbol: 'roundRect', + }) + ), + }) + + return chart +} + +const el = document.querySelector('canvas') +if (el) { + createChart(el) +} diff --git a/resources/assets/src/views/route.ts b/resources/assets/src/views/route.ts index dd750448..94966cf9 100644 --- a/resources/assets/src/views/route.ts +++ b/resources/assets/src/views/route.ts @@ -19,6 +19,10 @@ export default [ component: () => import('./user/Profile.vue'), el: '.content', }, + { + path: 'admin', + script: () => import('./admin/Dashboard'), + }, { path: 'admin/users', component: () => import('./admin/Users.vue'), diff --git a/resources/views/admin/index.blade.php b/resources/views/admin/index.blade.php index 240a7975..5dbbc981 100644 --- a/resources/views/admin/index.blade.php +++ b/resources/views/admin/index.blade.php @@ -67,14 +67,9 @@