pub struct WordCloud {
pub width: u32,
pub height: u32,
pub background: String,
pub words: Vec<PlacedWord>,
/* private fields */
}Fields§
§width: u32§height: u32§background: String§words: Vec<PlacedWord>Implementations§
Source§impl WordCloud
impl WordCloud
Sourcepub fn to_svg(&self) -> String
pub fn to_svg(&self) -> String
Examples found in repository?
examples/simple.rs (line 30)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6 let start = Instant::now();
7
8 let words = vec![
9 ("Rust", 100.0),
10 ("Performance", 80.0),
11 ("Safety", 70.0),
12 ("Concurrency", 60.0),
13 ("Fast", 50.0),
14 ("Memory", 45.0),
15 ("Efficient", 40.0),
16 ("Reliable", 35.0),
17 ("Community", 30.0),
18 ("Cargo", 25.0),
19 ("Crates", 20.0),
20 ("Macro", 15.0),
21 ];
22
23 println!("Generating word cloud with {} words...", words.len());
24
25 let wordcloud = generate(&words)?;
26
27 let png_data = wordcloud.to_png(2.0)?;
28 fs::write("output_simple.png", png_data)?;
29
30 let svg_data = wordcloud.to_svg();
31 fs::write("output_simple.svg", svg_data)?;
32
33 println!("Done! Saved to output_simple.png and output_simple.svg");
34 println!("Time elapsed: {:?}", start.elapsed());
35
36 Ok(())
37}Sourcepub fn to_png(&self, scale: f32) -> Result<Vec<u8>, Error>
pub fn to_png(&self, scale: f32) -> Result<Vec<u8>, Error>
Examples found in repository?
examples/advanced.rs (line 22)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 let words = vec![
6 WordInput::new("Custom", 90.0),
7 WordInput::new("Colors", 80.0),
8 WordInput::new("Seed", 70.0),
9 WordInput::new("Fixed", 60.0),
10 WordInput::new("Layout", 50.0),
11 ];
12
13 let wordcloud = WordCloudBuilder::new()
14 .size(600, 400)
15 .background("#1a1a1a")
16 .colors(vec!["#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#00FFFF"])
17 .seed(42)
18 .angles(vec![0.0])
19 .font_size_range(20.0, 100.0)
20 .build(&words)?;
21
22 fs::write("output_advanced.png", wordcloud.to_png(1.0)?)?;
23 println!("Generated advanced word cloud: output_advanced.png");
24
25 Ok(())
26}More examples
examples/simple.rs (line 27)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6 let start = Instant::now();
7
8 let words = vec![
9 ("Rust", 100.0),
10 ("Performance", 80.0),
11 ("Safety", 70.0),
12 ("Concurrency", 60.0),
13 ("Fast", 50.0),
14 ("Memory", 45.0),
15 ("Efficient", 40.0),
16 ("Reliable", 35.0),
17 ("Community", 30.0),
18 ("Cargo", 25.0),
19 ("Crates", 20.0),
20 ("Macro", 15.0),
21 ];
22
23 println!("Generating word cloud with {} words...", words.len());
24
25 let wordcloud = generate(&words)?;
26
27 let png_data = wordcloud.to_png(2.0)?;
28 fs::write("output_simple.png", png_data)?;
29
30 let svg_data = wordcloud.to_svg();
31 fs::write("output_simple.svg", svg_data)?;
32
33 println!("Done! Saved to output_simple.png and output_simple.svg");
34 println!("Time elapsed: {:?}", start.elapsed());
35
36 Ok(())
37}examples/mask_shape.rs (line 33)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 let output_path = "output_mask_heart.png";
6
7 let width = 800;
8 let height = 800;
9
10 println!("Generating words...");
11 let mut words = Vec::new();
12 for i in 0..300 {
13 words.push(WordInput::new("Love", 100.0));
14 words.push(WordInput::new("Rust", 80.0));
15 words.push(WordInput::new("Heart", 60.0 + (i as f32 % 40.0)));
16 words.push(WordInput::new("Mask", 40.0));
17 }
18
19 println!("Building word cloud...");
20
21 let scheme = ColorScheme::Default;
22 let wordcloud = WordCloudBuilder::new()
23 .size(width, height)
24 .mask_preset(MaskShape::Heart)
25 .color_scheme(scheme)
26 .background(scheme.background_color())
27 .padding(2)
28 .word_spacing(2.0)
29 .font_size_range(12.0, 80.0)
30 .build(&words)?;
31
32 println!("Saving to {}...", output_path);
33 fs::write(output_path, wordcloud.to_png(2.0)?)?;
34
35 println!("Done! Check {}", output_path);
36 Ok(())
37}examples/chinese_dense.rs (line 46)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7 let mut words = generate_dense_chinese_data();
8 println!("Generated {} Chinese words.", words.len());
9
10 let start = Instant::now();
11
12 let width = 1600;
13 let height = 1100;
14
15 println!("Building word cloud ({}x{})...", width, height);
16
17 let count_len = words.len();
18 let dynamic_max_font_size = if count_len < 20 {
19 150.0
20 } else if count_len < 50 {
21 120.0
22 } else {
23 100.0
24 };
25
26 let max_weight = words.first().map(|w| w.weight).unwrap_or(100.0);
27 for word in &mut words {
28 if max_weight > 100.0 {
29 word.weight = word.weight.sqrt() * 10.0;
30 }
31 }
32
33 let scheme = ColorScheme::Default;
34 let wordcloud = WordCloudBuilder::new()
35 .size(width, height)
36 .color_scheme(scheme)
37 .background(scheme.background_color())
38 .padding(2)
39 .angles(vec![0.0, 90.0])
40 .word_spacing(2.0)
41 .font_size_range(14.0, dynamic_max_font_size)
42 .seed(2025)
43 .build(&words)?;
44
45 let output_path = "output_chinese_dense.png";
46 fs::write(output_path, wordcloud.to_png(2.0)?)?;
47
48 println!("Success! Time elapsed: {:?}", start.elapsed());
49 println!("Saved to {}", output_path);
50
51 Ok(())
52}Auto Trait Implementations§
impl Freeze for WordCloud
impl RefUnwindSafe for WordCloud
impl Send for WordCloud
impl Sync for WordCloud
impl Unpin for WordCloud
impl UnwindSafe for WordCloud
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more