[英]Plotting a line graph by using data from an array of structs (c++)
一個文本文件包含 100 個學生,每行包括:名字、姓氏、考試 1、考試 2、考試 3。我將文本文件中的數據讀入結構數組。 但是現在我需要創建一個折線圖,其中 x 軸是學生人數,y 軸是他們三個考試成績中的每一個。 考試 1 為綠色,考試 2 為紅色,考試 3 為藍色。 並用水平線表示每次考試的平均值。 理論上它應該是這樣的。
這是我到目前為止想出的代碼,但我什至無法弄清楚從哪里開始創建折線圖。
#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();
}
您很可能希望使用圖形庫。 有一些可以為您繪制圖表。 還有其他人只能繪制形狀。
我使用 SFML 創建了這個丑陋的示例,因為它是我最熟悉的:
為了速度,我硬編碼了很多。
這是通過繪制邊框來工作的:
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);
創建頂點向量來存儲折線圖的折線部分:
std::vector<sf::Vertex> line_graph_1;
std::vector<sf::Vertex> line_graph_2;
std::vector<sf::Vertex> line_graph_3;
遍歷每個學生並將頂點添加到這些向量:
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);
在底部繪制名稱:
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);
在遍歷所有學生后,我繪制了線向量:
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);
最后繪制平均線:
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);
對於沒有內置圖形的圖形庫,類似的東西將是一個好的開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.