OpenGL——折线图柱状图饼图绘制
折线图绘制代码:
#include<iostream> //旧版本 固定管线
#include<Windows.h>
#include <GL/glut.h>
//新版本 可编程管线
/*#define GLEW_STATIC
#include <GL/glew.h>
#include<GLFW\glfw3.h>
*/ using namespace std; GLsizei winWidth = , winHeight = ;
GLint xRaster = , yRaster = ; GLubyte label[] = { 'J','a','n', 'F','e','b', 'M','a','r', 'A','p','r',
'M','a','y', 'J','u','n', 'J','u','l', 'A','u','g',
'S','e','p', 'O','c','t', 'N','o','v', 'D','e','c' }; GLint dataValue[] = { ,,,,,,,,,,, }; void init()
{
//窗口背景为白色
glClearColor(, , , );
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 610.0, 0.0, 500.0);
} void lineGraph()
{
GLint month, k;
GLint x = ; glClear(GL_COLOR_BUFFER_BIT);
//蓝色
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP); //画出折线段
for (k = ; k < ; k++) {
glVertex2i(x + k * , dataValue[k]);
}
glEnd(); //红色
glColor3f(1.0, 0.0, 0.0); //标注各点
for (k = ; k < ; k++) {
glRasterPos2i(xRaster + k * , dataValue[k] - );
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '*');
} //黑色
glColor3f(0.0, 0.0, 0.0); //横坐标说明
xRaster = ;
for (month = ; month < ; month++) {
glRasterPos2i(xRaster, yRaster);
for (k = * month;k < * month + ;k++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, label[k]);
xRaster += ;
}
}
glFlush();
} void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(, GLdouble(newWidth), , GLdouble(newHeight));
glClear(GL_COLOR_BUFFER_BIT);
} int main(int argc, char* argv[])
{ //对GLUT进行初始化,并处理所有的命令行参数
glutInit(&argc, argv);
//指定使用RGBA模式还是颜色索引模式,还可指定使用单缓冲还是双缓冲窗口
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
//指定了窗口左上角在屏幕上的位置
glutInitWindowPosition(, );
//制定了窗口大小(以像素为单位)
glutInitWindowSize(winWidth, winHeight);
//创建支持opengl渲染的窗口,在调用glutMainLoop函数前,窗口没有显示
glutCreateWindow("折线图");
init();
//显示回调函数
//每当GLUT确定一个窗口的内容需要重新显示时,通过glutDisplayFunc()注册的那个回调函数就会被执行
glutDisplayFunc(lineGraph);
glutReshapeFunc(winReshapeFcn);
//启动程序,所有窗口这时显示
glutMainLoop(); system("pause");
return ; }
运行结果:

柱状图代码:
void barChart()
{
GLint month, k; glClear(GL_COLOR_BUFFER_BIT); //红色
glColor3f(1.0, 0.0, 0.0);
for (k = ; k < ; k++) {
glRecti( + k * , , + k * , dataValue[k]);
} //黑色
glColor3f(0.0, 0.0, 0.0); //横坐标说明
xRaster = ;
for (month = ; month < ; month++) {
glRasterPos2i(xRaster, yRaster);
for (k = * month;k < * month + ;k++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, label[k]);
xRaster += ;
}
}
glFlush();
}
运行结果:

饼图代码:
#include<iostream>
#include <math.h>
//旧版本 固定管线
#include<Windows.h>
#include <GL/glut.h>
//新版本 可编程管线
/*#define GLEW_STATIC
#include <GL/glew.h>
#include<GLFW\glfw3.h>
*/ using namespace std; const GLdouble twoPi = 6.283185; class scrPt {
public:
scrPt() {
x = y = ;
}
GLint x, y;
void setCoords(GLint xCoordValue, GLint yCorrdValue) {
x = xCoordValue;
y = yCorrdValue;
}
GLint getx() const {
return x;
}
GLint gety() const {
return y;
}
void incrementx() {
x++;
}
void incrementy() {
y--;
} }; GLsizei winWidth = , winHeight = ; void init()
{
//窗口背景为白色
glClearColor(, , , );
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
} void setPixel(GLint xCoord, GLint yCoord)
{
glBegin(GL_POINTS);
glVertex2i(xCoord, yCoord);
glEnd();
} void circlePlotPoints(GLint xc, GLint yc, scrPt circPt)
{
setPixel(xc + circPt.getx(), yc + circPt.gety());
setPixel(xc - circPt.getx(), yc + circPt.gety());
setPixel(xc + circPt.getx(), yc - circPt.gety());
setPixel(xc - circPt.getx(), yc - circPt.gety());
setPixel(xc + circPt.gety(), yc + circPt.getx());
setPixel(xc - circPt.gety(), yc + circPt.getx());
setPixel(xc + circPt.gety(), yc - circPt.getx());
setPixel(xc - circPt.gety(), yc - circPt.getx());
} //中心画圆算法
void circleMidpoint(GLint xc, GLint yc, GLint radius)
{
scrPt circPt;
GLint p = - radius;//中点参数初值
circPt.setCoords(, radius);
circlePlotPoints(xc, yc, circPt);
while (circPt.getx() < circPt.gety()) {
circPt.incrementx();
if (p < )
p += * circPt.getx() + ;
else {
circPt.incrementy();
p += * (circPt.getx() - circPt.gety()) + ;
}
circlePlotPoints(xc, yc, circPt);
}
} void pieChart()
{
scrPt circCtr, piePt;
GLint radius = winWidth / ;
GLdouble sliceAngle, previousSliceAngle = 0.0; GLint k, nSlices = ; GLfloat dataValues[] = { 10.0,7.0,13.0,5.0,13.0,14.0,
3.0,16.0,5.0,3.0,17.0,8.0 };
GLfloat dataSum = 0.0; circCtr.x = winWidth / ;
circCtr.y = winHeight / ;
circleMidpoint(circCtr.x,circCtr.y, radius); for (k = ;k < nSlices;k++) {
dataSum += dataValues[k];
} for (k = ; k < ; k++) {
sliceAngle = twoPi * dataValues[k] / dataSum + previousSliceAngle;
piePt.x = circCtr.x + radius * cos(sliceAngle);
piePt.y = circCtr.y + radius * sin(sliceAngle);
glBegin(GL_LINES);
glVertex2i(circCtr.x, circCtr.y);
glVertex2i(piePt.x, piePt.y);
glEnd();
previousSliceAngle = sliceAngle;
} } void displayFcn()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
pieChart();
glFlush();
} void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));
glClear(GL_COLOR_BUFFER_BIT); winWidth = newWidth;
winHeight = newHeight;
} int main(int argc, char* argv[])
{ //对GLUT进行初始化,并处理所有的命令行参数
glutInit(&argc, argv);
//指定使用RGBA模式还是颜色索引模式,还可指定使用单缓冲还是双缓冲窗口
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
//指定了窗口左上角在屏幕上的位置
glutInitWindowPosition(, );
//制定了窗口大小(以像素为单位)
glutInitWindowSize(winWidth, winHeight);
//创建支持opengl渲染的窗口,在调用glutMainLoop函数前,窗口没有显示
glutCreateWindow("Pie");
init();
//显示回调函数
//每当GLUT确定一个窗口的内容需要重新显示时,通过glutDisplayFunc()注册的那个回调函数就会被执行
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFcn);
//启动程序,所有窗口这时显示
glutMainLoop(); system("pause");
return ; }
运行结果:

OpenGL——折线图柱状图饼图绘制的更多相关文章
- 百度推出的echarts,制表折线图柱状图饼图等的超级工具(转)
一.简介: 1.绘制数据图表,有了它,想要网页上绘制个折线图.柱状图,从此easy. 2.使用这个百度的echarts.js插件,是通过把图片绘制在canvas上在显示在页面上. 官网对echarts ...
- G2 基本使用 折线图 柱状图 饼图 基本配置
G2的基本使用 1.浏览器引入 <!-- 引入在线资源 --> <script src="https://gw.alipayobjects.com/os/lib/antv ...
- JavaScript数据可视化编程学习(一)Flotr2,包含简单的,柱状图,折线图,饼图,散点图
一.基础柱状图 二.基础的折线图 三.基础的饼图 四.基础的散点图 一.基础柱状图 如果你还没有想好你的数据用什么类型的图表来展示你的数据,你应该首先考虑是否可以做成柱状图.柱状图可以表示数据的变化过 ...
- 安卓图表引擎AChartEngine(三) - 示例源码折线图、饼图和柱状图
折线图: package org.achartengine.chartdemo.demo.chart; import java.util.ArrayList; import java.util.Lis ...
- HighCharts之2D柱状图、折线图和饼图的组合图
HighCharts之2D柱状图.折线图和饼图的组合图 1.实例源码 ColumnLinePie.html: <!DOCTYPE html> <html> <head&g ...
- 利用pandas读取Excel表格,用matplotlib.pyplot绘制直方图、折线图、饼图
利用pandas读取Excel表格,用matplotlib.pyplot绘制直方图.折线图.饼图 数据: 折线图代码: import pandas as pdimport matplotlib. ...
- 数据可视化(Echart) :柱状图、折线图、饼图等六种基本图表的特点及适用场合
数据可视化(Echart) 柱状图.折线图.饼图等六种基本图表的特点及适用场合 参考网址 效果图 源码 <!DOCTYPE html> <html> <head> ...
- v-charts 绘制柱状图、条形图、水球图、雷达图、折线图+柱状图,附官网地址
v-charts 官网地址:https://v-charts.js.org/#/ 柱状图: <template> <ve-histogram :data="chartDat ...
- ChartControl 折线图 柱状图
添加折线图(柱状图) 拖动ChartControl到Form上 在Series Collection中添加Line(或Bar) DevExpress.XtraCharts.Series series1 ...
随机推荐
- 调用 微信接口报错 {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
如下截图,仅为备份,本文转载地址: http://www.cnblogs.com/liaolongjun/p/6080240.html 以下正文↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ...
- Shell Step by Step (4) —— Cron & Echo
6.脚本定时任务 # Example of job definition: # .------------------------- minute (0 - 59) # | .------------ ...
- android:ProgressBar控件
ProgressBar 用于在界面上显示一个进度条,表示我们的程序正在加载一些数据.它的用 法也非常简单,修改 activity_main.xml 中的代码,如下所示: <LinearLayou ...
- java中四舍五入——double转BigDecimal的精度损失问题
代码: double d = -123456789012345.3426;//5898895455898954895989; NumberFormat nf = new DecimalFormat(& ...
- electron实现类似QQ来新消息时的闪烁与任务栏窗口提醒
公司项目有一款带即时聊天.群组功能的APP,因为要给客服人员使用,需要开发PC版本.之前使用C#开发过一个PC版本,但是C#的UI这一块支持的不太好,而且升级比较麻烦,我就牵头基于Electron去实 ...
- .NetCore中EFCore的使用整理(二)-关联表查询
EF常用处理关联加载的方式有3中:延迟加载(Lazy Loading).贪婪加载 (Eager Loading)以及显示加载. 一.EF Core 1.1 1.当前的版本,还不支持延迟加载(Lazy ...
- C++ stringstream 简化数据类型转换
C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性. 在C++中经常会使用到snprintf来格式化一些输 ...
- loopback文件系统
回环设备(loop-back devices) 实验环境 centos7.2 回环设备( 'loopback device')允许用户以一个普通磁盘文件虚拟一个块设备.(磁盘文件 --> 块设备 ...
- 微软BI 之SSIS 系列 - Execute SQL Task 中的 Single Row 与 Full Result Set 的处理技巧
开篇介绍 Execute SQL Task 这个控件在微软BI ETL 项目中使用的频率还是非常高的,也是大部分入门 SSIS 初学者最早接触到的几个控制流控件. 我们通常使用 Execute SQL ...
- 〖Android〗我的ADT Eclipse定制
1. 配置自动补全: Windows -> preferences -> 搜索assist,修改 java xml自动触发补全:.abcdefghijklmnopqrstuvwxyzABC ...