Skip to content

Commit

Permalink
feat:修复图表
Browse files Browse the repository at this point in the history
  • Loading branch information
konodioda727 committed Sep 19, 2024
1 parent 724bbdb commit 3c84be1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"dev:qq": "npm run build:qq -- --watch",
"dev:jd": "npm run build:jd -- --watch",
"dev:quickapp": "npm run build:quickapp -- --watch",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 40 --fix",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 90 --fix",
"prepare": "husky install",
"prettier": "prettier --write .",
"postinstall": "weapp-tw patch"
Expand Down
23 changes: 15 additions & 8 deletions src/common/components/chart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ const LineChart: React.FC<LineChartProps> = (props) => {
ctx.scale(1, width / 2 / height);
}
const barWidth = (width - 2 * padding) / data.length;
const lines = 5;
// 背景
drawRoundedRectangle(ctx, 0, 0, width, height, 10, () => null, '#f9f9f2');
// 标识线
ctx.beginPath();
ctx.strokeStyle = DEFAULT_MARK_LINE_COLOR;
ctx.lineWidth = 1;
for (let i = 0; i <= data.length; i++) {
const y = padding + (i * (height - 2 * padding)) / data.length;
for (let i = 0; i <= lines; i++) {
const y = padding + (i * (height - 2 * padding)) / lines;
ctx.beginPath();
ctx.moveTo(padding, y);
ctx.lineTo(width - padding, y);
Expand All @@ -101,9 +102,12 @@ const LineChart: React.FC<LineChartProps> = (props) => {
// 坐标轴标签
ctx.fillStyle = DEFAULT_TEXT_COLOR;
ctx.font = '10px sans-serif';
const step = Math.ceil(Math.max(...data) / data.length);
for (let i = 0; i <= data.length; i++) {
const y = height - padding - (i * (height - 2 * padding)) / data.length;
// 平均分配标签,比如五根标签,最大为94.4,那么标签最大为95,按19递增
const step = Math.floor(
((Math.floor(Math.max(...data) / lines) + 1) * lines) / lines
);
for (let i = 0; i <= lines; i++) {
const y = height - padding - (i * (height - 2 * padding)) / lines;
ctx.fillText((i * step).toString() + (subfix ? subfix : ''), 2, y + 3);
}
xLabels.forEach((value, index) => {
Expand All @@ -115,21 +119,24 @@ const LineChart: React.FC<LineChartProps> = (props) => {
ctx.strokeStyle = lineColor ?? DEFAULT_LINE_COLOR;
ctx.lineWidth = 6;
ctx.beginPath();
const dots: { x: number; y: number }[] = [];
data.forEach((value, index) => {
const x = padding + index * barWidth + BLANK;
const y =
height - padding - (value / (step * data.length)) * (height - 2 * padding);
const y = value
? height - padding - (value / (step * lines)) * (height - 2 * padding)
: height - padding;
if (index === 0) {
ctx.moveTo(x, y);
} else {
const prevX = padding + (index - 1) * barWidth;
const prevY = height - padding - (data[index - 1] / 70) * (height - 2 * padding);
const prevY = dots.at(-1)!.y;
const cp1x = (prevX + x) / 2;
const cp1y = prevY;
const cp2x = (prevX + x) / 2;
const cp2y = y;
ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
}
dots.push({ x, y });
});
ctx.stroke();

Expand Down
47 changes: 16 additions & 31 deletions src/pages/classInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* eslint-disable import/first */
import { Text, View } from '@tarojs/components';
import Taro from '@tarojs/taro';
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';

import './index.scss';

Expand Down Expand Up @@ -88,21 +88,6 @@ export default function Index() {

if (courseId) void getCommentData();
}, [courseId]);
// useEffect(() => {
// const fetchAnswer = async () => {
// try {
// await get(
// `/questions/list?biz=Course&biz_id=${courseId}&cur_question_id=&limit=`,
// true
// ).then((res) => {
// console.log(res);
// setQuestion(res.data);
// });
// } catch (e) {
// console.error('Failed to fetch course data:', e);
// }
// };
// }, []);
useEffect(() => {
console.log('test', courseId);
const fetchGrades = async () => {
Expand Down Expand Up @@ -133,21 +118,21 @@ export default function Index() {
void getNumData();
}
}, [courseId]);
const xLabels = useMemo(
() => ['0-40', '40-50', '50-60', '60-70', '70-80', '80-90', '90-100'],
[]
);
const { heightLightPercent, yData } = useMemo(() => {
const percent = (grade?.avg ?? 0) / 10;
return {
heightLightPercent: percent > 4 ? percent - 3 : 0,
yData: grade?.grades.map((item) => item.total_grades?.length ?? 0),
};
}, [grade]);
if (!course || !grade) {
return <Text>请先确定已签约成绩共享计划</Text>; // 数据加载中
}

const xLabels = ['0-40', '40-50', '50-60', '60-70', '70-80', '80-90', '90-100'];

const avgScore = grade.avg;
const heightLightIndex = Math.floor(avgScore / 10) - 4; // 假设 0-40 开始对应 index 0,每个区间跨度 10
// 处理 y 轴的数据,确保它们在 0 到 100 之间
const yData = grade?.grades?.flatMap((g) =>
g?.total_grades?.map((score) => score ?? 0)
);
// 计算高亮百分比
const heightLightPercent = heightLightIndex / xLabels.length;

const featuresList =
course.features && Array.isArray(course.features) ? course.features : [];

Expand All @@ -170,13 +155,13 @@ export default function Index() {
<Label3 key={keyindex} content={feature} />
))}
</View>
<View className="h-1/3 w-5/6 pt-1.5">
<View className="h-1/3 pt-1.5">
<LineChart
className="text-center"
className="mx-auto text-center"
data={yData}
xLabels={xLabels}
heightLightPercent={heightLightPercent}
title={`平均分: ${avgScore}`}
heightLightPercent={heightLightPercent ?? 0}
title={`平均分: ${grade?.avg.toFixed(1)}`}
/>
</View>
<View>
Expand Down

0 comments on commit 3c84be1

Please sign in to comment.