// Sets the xaxis and x2axis range int Plot::set_xrange_time(lua_State *L) { int64_t end = LuaHelpers::popi(L); int64_t start = LuaHelpers::popi(L); int64_t d = end - start; int64_t range[2]; range[0] = convepoch(1000 * floor(double(start) - 0.05 * d) / 1000); range[1] = convepoch(1000 * ceil(double(end) + 0.05 * d) / 1000); gcmd(str::printf("set xrange [%lld:%lld]", range[0], range[1])); gcmd(str::printf("set x2range [%lld:%lld]", range[0], range[1])); return 0; }
// Sets the xaxis and x2axis range int Plot::set_xrange(lua_State *L) { double end = LuaHelpers::popd(L); double start = LuaHelpers::popd(L); double d = end - start; double range[2]; range[0] = 1000 * floor(double(start) - 0.05 * d) / 1000; range[1] = 1000 * ceil(double(end) + 0.05 * d) / 1000; gcmd(str::printf("set xrange [%f:%f]", range[0], range[1])); gcmd(str::printf("set x2range [%f:%f]", range[0], range[1])); return 0; }
void debug (void) { if (savepc - 1 == breakpt1 || savepc - 1 == breakpt2) { print("Breakpoint\n"); savepc--; // Restore contents of breakpoint 1 if (breakpt1) mem8080[breakpt1] = brkdata1; breakpt1 = 0; // Restore contents of breakpoint 2 if (breakpt2) mem8080[breakpt2] = brkdata2; breakpt2 = 0; } xcmd(); listptr = savepc; for (;;) { int nchars; print("-"); nchars = read(0,cmdline,sizeof(cmdline)-1); if (nchars < 0) return; cmdline[nchars] = 0; for (cptr = cmdline; *cptr != '\0'; cptr++) if (*cptr >= 'a') *cptr -= 'a' - 'A'; cptr = cmdline; switch(*cptr++) { case 'G': if (gcmd()) return; return; // resume execution of 8080 program case 'D': dcmd(); break; case 'X': xcmd(); break; case 'L': lcmd(); break; case 'S': single_step (); // force single stepping return; // resume execution of 8080 program default: printf ("valid commands:\n"); printf (" g go\n"); printf (" d[NNNN] display memory [at address]\n"); printf (" x examine registers registers\n"); printf (" l[NNNN] disassemble instructions [at address]\n"); printf (" s single step\n"); } } }
// Sets the output file name and optionally the terminal type int Plot::set_output(lua_State *L) { std::string file, terminal; int width = 640, height = 480; if (lua_gettop(L) > 4) { return LuaHelpers::pushError(L, str::printf("Invalid number of arguments (expected 1-4, got %d)", lua_gettop(L))); } switch (lua_gettop(L)) { case 4: terminal = LuaHelpers::pops(L); case 3: height = LuaHelpers::popi(L); case 2: width = LuaHelpers::popi(L); default: file = LuaHelpers::pops(L); } if (terminal.empty()) { // Determine terminal type from extension or fall back to SVG size_t pos = file.find_last_of("."); if (pos != std::string::npos) { terminal = file.substr(pos+1); if (terminal.empty()) { terminal = m_standardTerminal; } } else { terminal = m_standardTerminal; } } if (terminal == "ps" || terminal == "eps") { terminal = "postscript eps color enhanced"; } else if (terminal == "jpg") { terminal = "jpeg"; } if (!file.empty()) { gcmd(str::printf("set output \"%s\"", file.c_str())); } else { gcmd(str::printf("set output")); } gcmd(str::printf("set terminal %s size %d,%d", terminal.c_str(), width, height)); return 0; }
void command(vector_t *** vecs) { int terminated = 0; int i = 0; int N = 0, rc = 0; int fr = 0, fsk = 0, fpr = 0, fskkv = 0, fkav = 0, max = 0; char input[N_MAX]; char *p, *p2, *output; vector_t * vec, * vec1; double val; vector_t ** vectors = *vecs; while (!terminated) { printf("\n> "); gets( input ); fr = fsk = fpr = fskkv = 0; for (i = 0; i < strlen(input); i ++) { if (input[i] == '=') fr = 1; if ((input[i] == '(') || (input[i] == ')')) fsk = 1; if (input[i] == ' ') fpr = 1; if (input[i] == '[') fskkv = 1; if (input[i] == '"') fkav = 1; } if ((fr == 0) && (fsk == 0) && (fpr == 0) && (fskkv == 0)) { if (strcmp("ls", input) == 0) ls(vectors); else if (strcmp("help", input) == 0) help(); else if (strcmp("quit", input) == 0) terminated = 1; else printf("Command is not found\n"); } if ((fr == 0) && (fsk == 1)) { p = strtok(input, "("); if (strcmp(p, "disp") == 0) { p = strtok(NULL, ")"); vec = vectors_search( vectors, p); if (vec) printf("dispersion %s = %f\n", p, disp(vec->size, vec->vals)); else printf("ERROR: Parameter '%s' not found\n", p ); } else if (strcmp(p, "avg") == 0) { p = strtok(NULL, ")"); vec = vectors_search( vectors, p); if (vec) printf("average %s = %f\n", p, avg(vec->size, vec->vals)); else printf("ERROR: Parameter '%s' not found\n", p ); } else if (strcmp(p, "print") == 0) { p = strtok(NULL, ")"); vec = vectors_search( vectors, p); if (vec) { printf("Vector %s: \n", p); vector_print(vec); } else printf("ERROR: Parameter '%s' not found\n", p ); } else if (strcmp(p, "load") == 0) { p = strtok(NULL, ")"); rc = load_csv(&vectors, p); if (rc == -1) printf("Failed to load file: %s \n", p); } else if (strcmp(p, "save") == 0) { p = strtok(NULL, ")"); rc = save_csv(vectors, p); if (rc == -1) printf("Failed to save file: %s \n", p); } else if (strcmp(p, "delete") == 0) { p = strtok(NULL, ")"); vec = vectors_search( vectors, p); if (vec) { vectors_remove(&vectors, vec); } else printf("ERROR: Parameter '%s' not found\n", p ); } else if (strcmp(p, "gnuplot") == 0) { p = strtok(NULL, ")"); gcmd(p); } else printf("Command is not found\n"); } if ((fr == 1) && (fskkv == 1)) { p = strtok(input, " ="); vec = (vector_t*) malloc (sizeof( vector_t ) ); vector_init( p, 0, vec ); p = strtok(NULL, "= [,]"); while ( p ) { val = strtod( p, &p2 ); if ( p == p2 ) { vector_clear( vec ); free( vec ); vec = 0; printf( "ERROR: Input data is invalid\n" ); break; } else { vector_append( vec, 1, &val ); } p = strtok(NULL, " ,]"); } if ( vec ) { vec1 = vectors_search( vectors, vec->name ); if (vec1) { vector_clear( vec1 ); vector_append( vec1, vec->size, vec->vals ); vector_clear( vec ); free( vec ); vec = 0; } else { vectors_add( &vectors, vec ); } } } if ((fr == 1) && (fskkv == 0)) { for (i = 0; i < vectors_count(vectors); i ++) if (max < vectors[i]->size) max = vectors[i]->size; printf("%d\n", max); printf("%d\n", vectors_count(vectors)); sergey(vectors, input, max, vectors_count(vectors)); } if ((fpr == 1) && (fskkv == 0) && (fsk == 0)) { p = strtok(input, " "); if (strcmp(p, "plot") == 0) { p = strtok(NULL, " "); vec = vectors_search(vectors, p); if (vec) { p = strtok(NULL, " "); vec1 = vectors_search(vectors, p); if (vec1) { p = strtok(NULL, " "); output = p; if (strcmp(p, "wxt") == 0) { output = NULL; } p = strtok(NULL, "."); N = atoi(p); gplot_vector(vec, vec1, output, N); } } } else printf("Command is not found\n"); } } *vecs = vectors; }
// Writes a Gnuplot command int Plot::cmd(lua_State *L) { gcmd(LuaHelpers::pops(L)); return 0; }
// Plots a pie chart int Plot::plot_pie(lua_State *L) { // Validate arguments if (lua_gettop(L) != 2) { return LuaHelpers::pushError(L, str::printf("Invalid number of arguments (expected 2, got %d)", lua_gettop(L))); } std::vector<double> values; std::vector<std::string> keys; switch (lua_gettop(L)) { default: values = LuaHelpers::popvd(L); keys = LuaHelpers::popvs(L); break; } if (keys.size() != values.size()) { return LuaHelpers::pushError(L, str::printf("Argument dimensions don't match (%d != %d)", keys.size(), values.size())); } // Prepare data, i.e. accumulate values to get [from,to] intervals size_t n = keys.size(); for (size_t i = 1; i < n; i++) { values[i] += values[i-1]; } // Open stream to data file std::ostringstream cmd; // First, print plot-specific data and helper functions to the file cmd << "set parametric\n" "set trange [0:1]\n" "set xrange [-1:1]\n" "set yrange [-1:1]\n" "set offsets 0.25,0.25,0.25,0.25\n" // For percentage labels "unset border\n" "unset tics\n" "p2rad(x)=pi * (x/0.5)\n" "fs(t,s,e)=sin(t * p2rad((e-s)) + p2rad(s))\n" "fc(t,s,e)=cos(t * p2rad((e-s)) + p2rad(s))" << std::endl; // Print percentage labels double last = 0; for (size_t i = 0; i < n; i++) { double m = last + (values[i] - last) / 2.0; cmd << "set label " << i+1 << " \"" << int(100 * (values[i] - last) + 0.5) << "%\" at first " << "1.05*cos(p2rad(" << m << ")),1.05*sin(p2rad(" << m << ")) front " << (m > 0.25 && m < 0.76 ? "right" : "left") << std::endl; last = values[i]; } // Plot arcs cmd << "plot \\" << std::endl; last = 0; for (size_t i = 0; i < n; i++) { cmd << " fc(t," << last << "," << values[i] << "),fs(t," << last << "," << values[i] << ") w filledcu xy=0,0 title " << "\"" << keys[i] << "\"" << (i != n-1 ? ",\\" : "") << std::endl; last = values[i]; } gcmd(cmd.str()); return 0; }
// Plots a histogram int Plot::plot_histogram(lua_State *L) { // Validate arguments int index = -1; if (lua_gettop(L) > 4) { return LuaHelpers::pushError(L, str::printf("Invalid number of arguments (expected 2-4, got %d)", lua_gettop(L))); } std::map<std::string, std::string> options; switch (lua_gettop(L)) { case 4: { if (lua_type(L, -1) == LUA_TTABLE) { options = LuaHelpers::popms(L); } else { options["style"] = LuaHelpers::pops(L); } } case 3: luaL_checktype(L, index--, LUA_TTABLE); default: luaL_checktype(L, index--, LUA_TTABLE); luaL_checktype(L, index--, LUA_TTABLE); break; } // First, read the keys (at index) ++index; std::vector<std::string> keys = LuaHelpers::topvs(L, index); // Check data entries size_t nseries = 0; ++index; if (LuaHelpers::tablesize(L, index) != keys.size()) { return LuaHelpers::pushError(L, str::printf("Number of keys and values doesn't match (%d != %d)", LuaHelpers::tablesize(L, index), keys.size())); } lua_pushvalue(L, index); lua_pushnil(L); if (lua_next(L, -2) != 0) { if (lua_type(L, -1) == LUA_TTABLE) { nseries = LuaHelpers::tablesize(L, -1); } else { nseries = 1; } lua_pop(L, 2); } lua_pop(L, 1); // Read titles (if any) ++index; std::vector<std::string> titles; if (index < 0) { titles = LuaHelpers::topvs(L, index); } gcmd("set style data histogram"); std::ostringstream cmd; cmd << "plot "; for (size_t i = 0; i < nseries; i++) { cmd << (i == 0 ? "'-'" : "''") << " using 2:xtic(1)"; if (titles.size() > i) { cmd << " title \"" << titles[i] << "\""; } else { cmd << " notitle"; } if (options.find("style") != options.end()) { cmd << " with " << options["style"]; } if (i < nseries-1) { cmd << ", "; } } PDEBUG << "Running plot with command: " << cmd.str() << endl; gcmd(cmd.str()); // Write data to pipe, separately for each series --index; std::ostringstream ss; for (size_t i = 0; i < nseries; i++) { ss.clear(); // Reset stringstream, but keep buffer ss.seekp(0); lua_pushvalue(L, index); lua_pushnil(L); int j = 0; while (lua_next(L, -2) != 0) { ss << '"' << keys[j++] << "\" "; if (lua_type(L, -1) == LUA_TTABLE) { if (nseries != LuaHelpers::tablesize(L, -1)) { return LuaHelpers::pushError(L, "Inconsistent number of series"); } lua_pushnumber(L, i+1); lua_gettable(L, -2); if (lua_type(L, -1) == LUA_TTABLE) { lua_pushnil(L); while (lua_next(L, -2) != 0) { ss << LuaHelpers::popd(L) << " "; } lua_pop(L, 1); } else { ss << LuaHelpers::popd(L); } lua_pop(L, 1); } else { ss << LuaHelpers::popd(L); } ss << "\n"; } lua_pop(L, 1); ss << "e\n"; // Marks end of data g->cmd(ss.str().c_str(), ss.tellp()); } return 0; }
// Plots multiple XY series int Plot::plot_multi_series(lua_State *L) { // Validate arguments int index = -1; if (lua_gettop(L) > 4) { return LuaHelpers::pushError(L, str::printf("Invalid number of arguments (expected 2-4, got %d)", lua_gettop(L))); } std::map<std::string, std::string> options; options.insert(std::pair<std::string, std::string>("style", "lines")); switch (lua_gettop(L)) { case 4: { if (lua_type(L, -1) == LUA_TTABLE) { options = LuaHelpers::popms(L); } else { options["style"] = LuaHelpers::pops(L); } } case 3: luaL_checktype(L, index--, LUA_TTABLE); default: luaL_checktype(L, index--, LUA_TTABLE); luaL_checktype(L, index--, LUA_TTABLE); break; } // Read data ++index; size_t nseries = LuaHelpers::tablesize(L, index); std::ostringstream *outs = new std::ostringstream[nseries]; for (size_t i = 0; i < nseries; i++) { std::ostringstream &out = outs[i]; // Read keys lua_rawgeti(L, index, i+1); std::vector<double> keys = LuaHelpers::popvd(L); // Check number of values ++index; lua_rawgeti(L, index, i+1); if (LuaHelpers::tablesize(L) != keys.size()) { return LuaHelpers::pushError(L, str::printf("Number of keys and values doesn't match (%d != %d)", LuaHelpers::tablesize(L, index), keys.size())); } // Avoid copying values via popvd() and read the directly luaL_checktype(L, -1, LUA_TTABLE); lua_pushvalue(L, -1); lua_pushnil(L); size_t j = 0; while (lua_next(L, -2) != 0) { out << keys[j++] << " " << LuaHelpers::popd(L) << std::endl; } lua_pop(L, 2); // Reset index back to keys --index; } // Read titles (if any) index += 2; std::vector<std::string> titles; if (index < 0) { titles = LuaHelpers::topvs(L, index); } std::ostringstream cmd; cmd << "plot "; for (size_t i = 0; i < nseries; i++) { cmd << "'-' using 1:2"; if (titles.size() > i) { cmd << " title \"" << titles[i] << "\""; } else { cmd << " notitle"; } if (options.find("style") != options.end()) { cmd << " with " << options["style"]; } if (i < nseries-1) { cmd << ", "; } } PDEBUG << "Running plot with command: " << cmd.str() << endl; gcmd(cmd.str()); // Write data for (size_t i = 0; i < nseries; i++) { g->cmd(outs[i].str()); g->cmd("e"); // Marks end of data } delete[] outs; return 0; }
// Sets the plot title int Plot::set_title(lua_State *L) { std::string title = LuaHelpers::pops(L); gcmd(str::printf("set title \"%s\"", title.c_str())); return 0; }