首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用结构数组中的数据绘制折线图(c++)

使用结构数组中的数据绘制折线图(c++)
EN

Stack Overflow用户
提问于 2020-11-08 16:30:50
回答 1查看 680关注 0票数 0

一个文本文件包含100名学生,每行包含:名字、姓氏、考试1、考试2、考试3。我将文本文件中的数据读取到一个结构数组中。但现在我需要创建一个线形图,其中包含x轴上的学生数量和y轴上的三个考试成绩。考试1为绿色,考试2为红色,考试3为蓝色。并用水平线表示每次检查的平均值。理论上它应该是这样的。

这是我到目前为止想出的代码,但我甚至不能确定从哪里开始创建折线图。

代码语言:javascript
复制
#include "library.h"
#include<iostream>
#include<cmath>
#include <cstdlib>
#include <fstream>
#include <string>
#include <iostream>

using namespace std;

struct StudentInfo {
string first;
string last;
int e1;
int e2;
int e3;
};

void main()
{
 
ifstream in_file;

in_file.open("exams.txt");
StudentInfo students[100];
int count = 0;

while(in_file >> students[count].first){
    in_file >> students[count].last;
    in_file >> students[count].e1;
    in_file >> students [count].e2;
    in_file >> students [count].e3;
    count ++;
}

for (int i = 0; i < count; i++){
 
    cout << students[i].first << " " << students[i].last << " " << students[i].e1 << " " << students[i].e2 << " " << students[i].e3 << endl;
}

in_file.close();
}
EN

回答 1

Stack Overflow用户

发布于 2020-11-09 01:47:02

您很可能希望使用图形库。有一些工具可以为您绘制图表。还有一些人只能画出形状。

我使用SFML创建了这个丑陋的示例,因为它是我最熟悉的:

为了提高速度,我硬编码了很多。

这是通过绘制边框来实现的:

代码语言:javascript
复制
        std::array<sf::Vertex, 3> border {
            sf::Vertex(sf::Vector2f(0.f, 0.f), sf::Color::Black),
            sf::Vertex(sf::Vector2f(0.f, 100.f), sf::Color::Black),
            sf::Vertex(sf::Vector2f(full_width, 100.f), sf::Color::Black)
        };
        window.draw(border.data(), border.size(), sf::PrimitiveType::LineStrip);

创建顶点矢量以存储折线图的折线部分:

代码语言:javascript
复制
        std::vector<sf::Vertex> line_graph_1;
        std::vector<sf::Vertex> line_graph_2;
        std::vector<sf::Vertex> line_graph_3;

遍历每个学生并将顶点添加到这些向量中:

代码语言:javascript
复制
            line_graph_1.emplace_back(sf::Vector2f(x, 100.f - student.e1), sf::Color::Red);
            line_graph_2.emplace_back(sf::Vector2f(x, 100.f - student.e2), sf::Color::Green);
            line_graph_3.emplace_back(sf::Vector2f(x, 100.f - student.e3), sf::Color::Blue);

在底部绘制名称:

代码语言:javascript
复制
            text.setString(student.first + ' ' + student.last);
            const auto bounds = text.getLocalBounds();
            text.setPosition(
                x + bounds.left - bounds.width / 2.f,
                128.f + bounds.top - bounds.height / 2.f
            );
            window.draw(text);

在遍历所有学生之后,我绘制了线向量:

代码语言:javascript
复制
        window.draw(line_graph_1.data(), line_graph_1.size(), sf::PrimitiveType::LineStrip);
        window.draw(line_graph_2.data(), line_graph_2.size(), sf::PrimitiveType::LineStrip);
        window.draw(line_graph_3.data(), line_graph_3.size(), sf::PrimitiveType::LineStrip);

最后画出平均线:

代码语言:javascript
复制
        std::array<sf::Vertex, 3> average_line{
            sf::Vertex(sf::Vector2f(0.f, 100.f - average_1), sf::Color::Red),
            sf::Vertex(sf::Vector2f(full_width, 100.f - average_1), sf::Color::Red)
        };
        window.draw(average_line.data(), average_line.size(), sf::PrimitiveType::Lines);
        average_line = {
            sf::Vertex(sf::Vector2f(0.f, 100.f - average_2), sf::Color::Green),
            sf::Vertex(sf::Vector2f(full_width, 100.f - average_2), sf::Color::Green)
        };
        window.draw(average_line.data(), average_line.size(), sf::PrimitiveType::Lines);
        average_line = {
            sf::Vertex(sf::Vector2f(0.f, 100.f - average_3), sf::Color::Blue),
            sf::Vertex(sf::Vector2f(full_width, 100.f - average_3), sf::Color::Blue)
        };
        window.draw(average_line.data(), average_line.size(), sf::PrimitiveType::Lines);

对于没有内置图形的图形库来说,类似的东西将是一个很好的开始。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64736166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档