c语言绘制折线图 linux
时间: 2023-08-10 07:55:38 浏览: 181
你可以使用C语言和一些图形库来绘制折线图在Linux系统上。在Linux中,常用的图形库包括Xlib、GTK+、Qt等。下面是一个使用Xlib库绘制折线图的简单示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#define WIDTH 500
#define HEIGHT 300
void drawLine(Display *display, Window window, GC gc, int x1, int y1, int x2, int y2) {
XDrawLine(display, window, gc, x1, y1, x2, y2);
XFlush(display);
}
int main() {
Display *display;
Window window;
XEvent event;
GC gc;
display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}
int screen = DefaultScreen(display);
unsigned long black = BlackPixel(display, screen);
unsigned long white = WhitePixel(display, screen);
window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, WIDTH, HEIGHT, 0, black, white);
XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);
gc = XCreateGC(display, window, 0, NULL);
XSetForeground(display, gc, black);
while (1) {
XNextEvent(display, &event);
if (event.type == Expose) {
drawLine(display, window, gc, 50, 50, 450, 50); // 横线
drawLine(display, window, gc, 50, 50, 50, 250); // 竖线
drawLine(display, window, gc, 50, 250, 450, 250); // 横线
drawLine(display, window, gc, 450, 50, 450, 250); // 竖线
drawLine(display, window, gc, 100, 200, 200, 100); // 折线
XFlush(display);
}
if (event.type == KeyPress)
break;
}
XFreeGC(display, gc);
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}
```
这段代码使用Xlib库创建一个窗口,并在窗口中绘制一个简单的折线图。你可以根据自己的需求修改代码,实现更复杂的折线图功能。记得在编译时链接Xlib库,命令如下:
```
gcc your_code.c -o your_executable -lX11
```
编译后运行可执行文件即可在Linux系统上看到绘制的折线图。