Exemplo n.º 1
0
func AddLinePointsWithColor(plt *plot.Plot, i int, vs ...interface{}) error {
	var ps []plot.Plotter
	names := make(map[[2]plot.Thumbnailer]string)
	name := ""
	for _, v := range vs {
		switch t := v.(type) {
		case string:
			name = t

		case plotter.XYer:
			l, s, err := plotter.NewLinePoints(t)
			if err != nil {
				return err
			}
			l.Color = plotutil.Color(i)
			l.Dashes = plotutil.Dashes(i)
			s.Color = plotutil.Color(i)
			s.Shape = plotutil.Shape(i)
			ps = append(ps, l, s)
			if name != "" {
				names[[2]plot.Thumbnailer{l, s}] = name
				name = ""
			}

		default:
			panic(fmt.Sprintf("AddLinePointsWithColor handles strings and plotter.XYers, got %T", t))
		}
	}
	plt.Add(ps...)
	for ps, n := range names {
		plt.Legend.Add(n, ps[0], ps[1])
	}
	return nil
}
Exemplo n.º 2
0
func col(n sim.Nucleus) color.Color {
	switch n {
	case sim.Nucleus{A: 12, Z: 6}:
		return rgb(0, 0, 0)
	case sim.Nucleus{A: 16, Z: 8}:
		return rgb(0, 0, 255)
	case sim.Nucleus{A: 24, Z: 12}:
		return rgb(0, 255, 0)
	case sim.Nucleus{A: 28, Z: 14}:
		return rgb(0, 128, 255)
	case sim.Nucleus{A: 32, Z: 16}:
		return rgb(255, 255, 51)
	case sim.Nucleus{A: 36, Z: 18}:
		return rgb(128, 128, 128)
	case sim.Nucleus{A: 40, Z: 20}:
		return rgb(192, 192, 192)
	case sim.Nucleus{A: 44, Z: 22}:
		return rgb(255, 0, 255)
	case sim.Nucleus{A: 48, Z: 24}:
		return rgb(51, 255, 255)
	case sim.Nucleus{A: 52, Z: 26}:
		return rgb(255, 165, 0)
	case sim.Nucleus{A: 56, Z: 28}:
		return rgb(255, 0, 0)
	}
	return plotutil.Color(n.A)
}
Exemplo n.º 3
0
// CreateImage creates graph of nyanpass
func (n *Nyanpass) CreateImage(fileName string) error {
	if n.Counts == nil {
		return errors.New("Count is not defined.")
	}

	p, err := plot.New()
	if err != nil {
		return err
	}

	bar, err := plotter.NewBarChart(n.Counts, vg.Points(30))
	if err != nil {
		return err
	}
	bar.LineStyle.Width = vg.Length(0)
	bar.Color = plotutil.Color(2)

	p.Add(bar)
	p.Title.Text = "Nyanpass Graph"
	p.X.Label.Text = "Days"
	p.Y.Label.Text = "Nyanpass count"
	p.NominalX(n.labels...)
	p.Y.Tick.Marker = RelabelTicks{}

	if err := p.Save(6*vg.Inch, 6*vg.Inch, fileName); err != nil {
		return err
	}
	n.imagePath = fileName
	return nil
}
Exemplo n.º 4
0
func (plt *plttr) addDiscrete(name string, sig Discrete) {
	// TODO there appears to be a bug in gonum plot where certain
	// dashed lines for a particular result will not render correctly.
	// Raw calls of plotutil are just tossed in here for now and avoids
	// dashed lines.
	l, err := plotter.NewLine(xyer([]float64(sig)))
	if err != nil {
		panic(err)
	}
	l.Color = plotutil.Color(plt.nlines)
	// l.Dashes = plotutil.Dashes(plt.nlines)

	plt.Add(l)
	plt.Legend.Add(name, l)

	plt.nlines++
}
Exemplo n.º 5
0
func plotTableSizes(sess *r.Session) {
	sizes := []float64{}
	for _, t := range tables {
		sizes = append(sizes, float64(averageDocumentSize(sess, t)))
	}

	p, _ := plot.New()
	p.Title.Text = "Average document sizes"
	p.Y.Label.Text = "Size in bytes"

	w := vg.Points(20)
	bars, _ := plotter.NewBarChart(plotter.Values(sizes), w)
	bars.Color = plotutil.Color(0)
	bars.LineStyle.Width = vg.Length(0)

	p.Add(bars)
	p.NominalX(tables...)

	p.Save(6*vg.Inch, 6*vg.Inch, "avg_doc_sizes.png")
}
Exemplo n.º 6
0
func marshalPNG(r *http.Request, results []*metricData) []byte {
	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	// set bg/fg colors
	bgcolor := string2Color(getString(r.FormValue("bgcolor"), "black"))
	p.BackgroundColor = bgcolor

	fgcolorstr := getString(r.FormValue("fgcolor"), "white")
	fgcolor := string2Color(fgcolorstr)
	p.Title.Color = fgcolor
	p.X.LineStyle.Color = fgcolor
	p.Y.LineStyle.Color = fgcolor
	p.X.Tick.LineStyle.Color = fgcolor
	p.Y.Tick.LineStyle.Color = fgcolor
	p.X.Tick.Label.Color = fgcolor
	p.Y.Tick.Label.Color = fgcolor
	p.X.Label.Color = fgcolor
	p.Y.Label.Color = fgcolor
	p.Legend.Color = fgcolor

	// set grid
	grid := plotter.NewGrid()
	grid.Vertical.Color = fgcolor
	grid.Horizontal.Color = fgcolor
	p.Add(grid)

	// line mode (ikruglow) TODO check values
	lineMode := getString(r.FormValue("lineMode"), "slope")

	// width and height
	width := getFloat64(r.FormValue("width"), 330)
	height := getFloat64(r.FormValue("height"), 250)

	// need different timeMarker's based on step size
	p.Title.Text = r.FormValue("title")
	p.X.Tick.Marker = NewTimeMarker(results[0].GetStepTime())

	hideLegend := getBool(r.FormValue("hideLegend"), false)

	graphOnly := getBool(r.FormValue("graphOnly"), false)
	if graphOnly {
		p.HideAxes()
	}

	if len(results) == 1 && results[0].color == "" {
		results[0].color = fgcolorstr
	}

	var lines []plot.Plotter
	for i, r := range results {
		l := NewResponsePlotter(r)

		l.LineStyle.Color = fgcolor

		// consolidate datapoints
		l.maybeConsolidateData(width)

		if r.drawAsInfinite {
			l.lineMode = "drawAsInfinite"
		} else {
			l.lineMode = lineMode
		}

		if r.color != "" {
			l.Color = string2Color(r.color)
		} else {
			l.Color = plotutil.Color(i)
		}

		lines = append(lines, l)

		if !graphOnly && !hideLegend {
			p.Legend.Add(r.GetName(), l)
		}
	}

	p.Add(lines...)

	p.Y.Max *= 1.05
	p.Y.Min *= 0.95

	writerTo, err := p.WriterTo(vg.Points(width), vg.Points(height), "png")
	var buffer bytes.Buffer
	if _, err := writerTo.WriteTo(&buffer); err != nil {
		panic(err)
	}

	return buffer.Bytes()
}
Exemplo n.º 7
0
func (p *chartPlugin) messageFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
	if service.IsMe(message) {
		return
	}

	if bruxism.MatchesCommand(service, "chart", message) {
		query, parts := bruxism.ParseCommand(service, message)
		if len(parts) == 0 {
			service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart eg: %s", p.randomChart(service)))
			return
		}

		start, end := 0.5, 0.5

		switch parts[0] {
		case "up":
			start, end = 0, 1
		case "down":
			start, end = 1, 0
		case "flat":
		case "straight":
		default:
			service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart direction. eg: %s", p.randomChart(service)))
			return
		}

		axes := strings.Split(query[len(parts[0]):], ",")
		if len(axes) != 2 {
			service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart axis labels eg: %s", p.randomChart(service)))
			return
		}

		pl, err := plot.New()
		if err != nil {
			service.SendMessage(message.Channel(), fmt.Sprintf("Error making chart, sorry! eg: %s", p.randomChart(service)))
			return
		}

		service.Typing(message.Channel())

		pl.Y.Label.Text = axes[0]
		pl.X.Label.Text = axes[1]

		num := 5 + rand.Intn(15)

		start *= float64(num)
		end *= float64(num)

		pts := make(plotter.XYs, num)
		for i := range pts {
			pts[i].X = float64(i) + rand.Float64()*0.5 - 0.2
			pts[i].Y = start + float64(end-start)/float64(num-1)*float64(i) + rand.Float64()*0.5 - 0.25
		}

		pl.X.Tick.Label.Color = color.Transparent
		pl.Y.Tick.Label.Color = color.Transparent

		pl.X.Min = -0.5
		pl.X.Max = float64(num) + 0.5

		pl.Y.Min = -0.5
		pl.Y.Max = float64(num) + 0.5

		lpLine, lpPoints, err := plotter.NewLinePoints(pts)
		if err != nil {
			service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem creating your chart.", message.UserName()))
		}
		lpLine.Color = plotutil.Color(rand.Int())
		lpLine.Width = vg.Points(1 + 0.5*rand.Float64())
		lpLine.Dashes = plotutil.Dashes(rand.Int())
		lpPoints.Shape = plotutil.Shape(rand.Int())
		lpPoints.Color = lpLine.Color

		pl.Add(lpLine, lpPoints)

		w, err := pl.WriterTo(320, 240, "png")
		if err != nil {
			service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem creating your chart.", message.UserName()))
			return
		}

		b := &bytes.Buffer{}
		w.WriteTo(b)

		go func() {
			if service.Name() == bruxism.DiscordServiceName {
				discord := service.(*bruxism.Discord)
				p, err := discord.UserChannelPermissions(message.UserID(), message.Channel())
				if err == nil && p&discordgo.PermissionAttachFiles != 0 {
					service.SendFile(message.Channel(), "chart.png", b)
					return
				}
			}

			url, err := bot.UploadToImgur(b, "chart.png")
			if err != nil {
				service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem uploading the chart to imgur.", message.UserName()))
				log.Println("Error uploading chart: ", err)
				return
			}

			if service.Name() == bruxism.DiscordServiceName {
				service.SendMessage(message.Channel(), fmt.Sprintf("Here's your chart <@%s>: %s", message.UserID(), url))
			} else {
				service.SendMessage(message.Channel(), fmt.Sprintf("Here's your chart %s: %s", message.UserName(), url))
			}
		}()
	}
}