- 定义一个 MybatisPlusMapper 接口,用于查询需要绘制折线图的数据。在这个示例中,我们查询了 MaterialTest 实体类的测试结果数据
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface MaterialTestMapper extends BaseMapper<MaterialTest> {
List<ChartData> selectChartData(Page<?> page, @Param("materialTest") MaterialTest materialTest);
}
2.创建一个 ChartData 类,该类用于封装绘制折线图所需的数据。在这个示例中,我们定义了 categoryName、seriesName 和 value 三个字段,分别表示 X 轴分类名称、系列名称和数据值。
import lombok.Data;
@Data
public class ChartData {
private String categoryName;
private String seriesName;
private double value;
}
3.在控制器类中注入 MybatisPlusMapper 对象,并定义一个 GET 请求处理方法,用于返回数据和模板页面。在这个示例中,我们使用 RuoYi Admin 提供的 ChartUtils.createLineChart() 方法创建了一个折线图,并将数据绑定到模板页面中。
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/chart")
public class ChartController extends BaseController {
private String prefix = "chart";
@Autowired
private MaterialTestMapper materialTestMapper;
@RequiresPermissions("chart:line:view")
@GetMapping("/line")
public String line(ModelMap mmap) {
Page<?> page = getPage();
List<MaterialTest> materialTests = materialTestMapper.selectPage(page, null).getRecords();
List<ChartData> chartDataList = materialTestMapper.selectChartData(page, null);
mmap.put("chart", ChartUtils.createLineChart(chartDataList));
mmap.put("materialTests", materialTests);
return prefix + "/line";
}
}
在首先查询需要绘制折线图的数据,
并用 ChartUtils.createLineChart() 方法创建了一个折线图对象。
然后,我们将数据和折线图对象绑定到模板页面中,最后返回模板页面。
文章介绍了如何通过定义一个MybatisPlusMapper接口查询MaterialTest实体类的测试结果数据,创建ChartData类封装折线图数据,并在控制器中使用ChartUtils创建折线图,将数据绑定到模板页面展示。
3320

被折叠的 条评论
为什么被折叠?



